作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
下面来看这句抽象的代码:
$page_callback_result = call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
首先我们需要知道,$router_item['page_callback']和$router_item['page_arguments']的值在当前情况下是什么。现在的路径为node/1。它对应的:
$router_item['page_callback'] : node_page_view
$router_item['page_arguments'] : array(1)
注意,这里的array(1),表示arg(1),Drupal向node_page_view传递参数的时候,会做以下工作:
$nid = arg(1);
$node = node_load($nid);
对于node/1/edit,arg(0)就是node,arg(1)就是1,arg(2)就是edit,依次类推。这个参数的传递过程,是Drupal菜单系统的一种特有机制,我们了解就可以了,知道这里传递过来的是$node对象。
我们把前面抽象的函数,换成具体的情况:
$page_callback_result = call_user_func_array(‘node_page_view’, array($node));
call_user_func_array是一个PHP函数,我刚开始看到这个函数的时候,感到头大。查了一下文档,明白了它是用来动态的调用函数的。上面的这行代码,就等价于:
call_user_func_array(‘node_page_view’, array($node)) = node_page_view($node);
实际上,就是这样的:
$page_callback_result = node_page_view($node);
这是当路径为node/1时的情况。当路径为node/1/edit的时候,执行到这里,实际情况是这样的:
$page_callback_result = node_page_edit($node);
这里具体执行的这个回调函数,是动态的,路径不一样,对应的回调函数也不一样。