2.6. 分发器
来源:互联网 发布:淘宝棉皮鞋 编辑:程序博客网 时间:2024/05/21 10:13
2.6.1. 概述
分发是取得请求对象,提取其中的模块名,控制器名,动作名以及可选参数,然后实例化控制器并调用其中的动作的整过过程。如果其中的模块、控制器或者动作没能找到,将使用它们的默认值。Zend_Controller_Dispatcher_Standard指定每个控制器和动作的默认值为 index,模块的默认值为default,允许开发人通过setDefaultController()、setDefaultAction()和setDefaultModule()改变默认值设定。
分发发生在前端控制器中的一个循环(loop)中。分发之前,前端控制器通过路由请求,找到用户指定的模块、控制器、动作和可选参数。然后进入分发循环,分发请求。
每次迭代(iteration)过程开始时,在请求对象中设置一个标志指示该动作已分发。如果在动作或者前/后分发(pre/postDispatch)插件重置了该标志,分发循环将继续下去并试图分发新的请求。通过改变请求中的控制器或者动作并重置已分发标志,开发人员可以定制执行一个请求链。
控制这种分发过程的动作控制器方法是_forward();在任意的pre/postDispatch()或者动作中调用该方法,并传入动作、控制器、模块、以及可选的附加参数,就可以进入新的动作。
<?phppublic function fooAction(){ // forward to another action in the current controller and module: $this->_forward('bar', null, null, array('baz' => 'bogus'));}public function barAction(){ // forward to an action in another controller, FooController::bazAction(), // in the current module: $this->_forward('baz', 'foo', null, array('baz' => 'bogus'));}public function bazAction(){ // forward to an action in another controller in another module, // Foo_BarController::bazAction(): $this->_forward('baz', 'bar', 'foo', array('baz' => 'bogus'));}
2.6.2. 子类化分发器
Zend_Controller_Front首先调用路由器找到请求中的第一个动作,然后进入分发循环,调用分发器分发动作。
分发器需要大量数据完成任务——它需要知道如何格式化控制器和动作的名称,到哪儿找到控制器类文件,模块名是否有效,以及基于其它可用信息判定请求是否能分发的API。
Zend_Controller_Dispatcher_Interface定义了下列所有分发器需要实现的方法。
interface Zend_Controller_Dispatcher_Interface{ /** * Format a string into a controller class name. * * @param string $unformatted * @return string */ public function formatControllerName($unformatted); /** * Format a string into an action method name. * * @param string $unformatted * @return string */ public function formatActionName($unformatted); /** * Determine if a request is dispatchable * * @param Zend_Controller_Request_Abstract $request * @return boolean */ public function isDispatchable(Zend_Controller_Request_Abstract $request); /** * Set a user parameter (via front controller, or for local use) * * @param string $name * @param mixed $value * @return Zend_Controller_Dispatcher_Interface */ public function setParam($name, $value); /** * Set an array of user parameters * * @param array $params * @return Zend_Controller_Dispatcher_Interface */ public function setParams(array $params); /** * Retrieve a single user parameter * * @param string $name * @return mixed */ public function getParam($name); /** * Retrieve all user parameters * * @return array */ public function getParams(); /** * Clear the user parameter stack, or a single user parameter * * @param null|string|array single key or array of keys for params to clear * @return Zend_Controller_Dispatcher_Interface */ public function clearParams($name = null); /** * Set the response object to use, if any * * @param Zend_Controller_Response_Abstract|null $response * @return void */ public function setResponse(Zend_Controller_Response_Abstract $response = null); /** * Retrieve the response object, if any * * @return Zend_Controller_Response_Abstract|null */ public function getResponse(); /** * Add a controller directory to the controller directory stack * * @param string $path * @param string $args * @return Zend_Controller_Dispatcher_Interface */ public function addControllerDirectory($path, $args = null); /** * Set the directory (or directories) where controller files are stored * * @param string|array $dir * @return Zend_Controller_Dispatcher_Interface */ public function setControllerDirectory($path); /** * Return the currently set directory(ies) for controller file lookup * * @return array */ public function getControllerDirectory(); /** * Dispatch a request to a (module/)controller/action. * * @param Zend_Controller_Request_Abstract $request * @param Zend_Controller_Response_Abstract $response * @return Zend_Controller_Request_Abstract|boolean */ public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response); /** * Whether or not a given module is valid * * @param string $module * @return boolean */ public function isValidModule($module);}
不过大多数情况下,只需要简单地扩展抽象类Zend_Controller_Dispatcher_Abstract,其中已经定义好了上面的大部分方法。或者扩展Zend_Controller_Dispatcher_Standard类,基于标准分发器来修改功能。
需要子类化分发器的可能原因包括:期望在动作控制器中使用不同的类和方法命名模式,或者期望使用不同的分发方式,比如分发到控制器目录下的动作文件,而不是控制器类的动作方法。
- 2.6. 分发器
- as3 消息分发器
- 事件分发器
- 事件分发器
- 浅学springmvc 分发器
- zabbix-agent 分发器
- 分发器与WEBrick服务器
- SPRING分发器(DispatcherServlet)
- cocos2dx的事件分发器
- cocos2dx的事件分发器
- Apache+Jetty,做自己的分发器
- Zend Framework教程-Zend_Controller_Dispatcher分发器
- Tornado基于方法的url分发器
- cocos2d-x触摸分发器原理
- RequestDispatcher请求分发器的使用方法
- Cocos2d-x触摸分发器原理
- spring mvc 分发器(DispatcherServlet)
- cocos2d-x触摸分发器原理
- GCC/G++编译器以及GDB调试
- Android屏幕适配全攻略
- 学习两天python后感
- DEC 加密和解密
- 埃及分数
- 2.6. 分发器
- 第一篇博客
- 你不努力,谁也给不了你想要的生活
- HDU 2473 删除并查集(装逼遭天谴)
- 网络寻路
- 简述FPS的计算方法
- 归并排序C++
- 文件复制
- 在ios中举个简单的protocol例子,关于两个类用协议方式传值。