Zend_Controller-Dispatcher.xml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <sect1 id="zend.controller.dispatcher">
  2. <title>分发器</title>
  3. <sect2 id="zend.controller.dispatcher.overview">
  4. <title>概述</title>
  5. <para>
  6. 分发是取得请求对象,提取其中的模块名,控制器名,动作名以及可选参数,然后实例化控制器并调用其中的动作的整过过程。如果其中的模块、控制器或者动作没能找到,将使用它们的默认值。<code>Zend_Controller_Dispatcher_Standard</code>指定每个控制器和动作的默认值为 <code>index</code>,模块的默认值为<code>default</code>,允许开发人通过<code>setDefaultController()</code>、<code>setDefaultAction()</code>和<code>setDefaultModule()</code>改变默认值设定。
  7. </para>
  8. <note>
  9. <title>缺省模块</title>
  10. <para>
  11. 当创建模块程序,你可能也需要缺省模块的命名空间(缺省配置中,缺省模块<emphasis>没有</emphasis>命名空间)。从 1.5.0 开始,可以在前端控制器或你的派遣器里通过指定 <code>prefixDefaultModule</code> 为 true 来实现。
  12. </para>
  13. <programlisting role="php"><![CDATA[
  14. // 在你的前端控制器中:
  15. $front->setParam('prefixDefaultModule', true);
  16. // 在你的分发器中:
  17. $dispatcher->setParam('prefixDefaultModule', true);
  18. ]]>
  19. </programlisting>
  20. <para>
  21. 这允许你重定义一个已存在的模块为程序的缺省模块。
  22. </para>
  23. </note>
  24. <para>
  25. 分发发生在前端控制器中的一个循环(loop)中。分发之前,前端控制器通过路由请求,找到用户指定的模块、控制器、动作和可选参数。然后进入分发循环,分发请求。
  26. </para>
  27. <para>
  28. 每次迭代(iteration)过程开始时,在请求对象中设置一个标志指示该动作已分发。如果在动作或者前/后分发(pre/postDispatch)插件重置了该标志,分发循环将继续下去并试图分发新的请求。通过改变请求中的控制器或者动作并重置已分发标志,开发人员可以定制执行一个请求链。
  29. </para>
  30. <para>
  31. 控制这种分发过程的动作控制器方法是<code>_forward()</code>;在任意的<code>pre/postDispatch()</code>或者动作中调用该方法,并传入动作、控制器、模块、以及可选的附加参数,就可以进入新的动作。
  32. </para>
  33. <programlisting role="php"><![CDATA[
  34. public function fooAction()
  35. {
  36. // forward to another action in the current controller and module:
  37. $this->_forward('bar', null, null, array('baz' => 'bogus'));
  38. }
  39. public function barAction()
  40. {
  41. // forward to an action in another controller:
  42. // FooController::bazAction(),
  43. // in the current module:
  44. $this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
  45. }
  46. public function bazAction()
  47. {
  48. // forward to an action in another controller in another module,
  49. // Foo_BarController::bazAction():
  50. $this->_forward('baz', 'bar', 'foo', array('baz' => 'bogus'));
  51. }
  52. ]]>
  53. </programlisting>
  54. </sect2>
  55. <sect2 id="zend.controller.dispatcher.subclassing">
  56. <title>子类化分发器</title>
  57. <para>
  58. <code>Zend_Controller_Front</code>首先调用路由器找到请求中的第一个动作,然后进入分发循环,调用分发器分发动作。
  59. </para>
  60. <para>
  61. 分发器需要大量数据完成任务——它需要知道如何格式化控制器和动作的名称,到哪儿找到控制器类文件,模块名是否有效,以及基于其它可用信息判定请求是否能分发的API。
  62. </para>
  63. <para>
  64. <code>Zend_Controller_Dispatcher_Interface</code>定义了下列所有分发器需要实现的方法。
  65. </para>
  66. <programlisting role="php"><![CDATA[
  67. interface Zend_Controller_Dispatcher_Interface
  68. {
  69. /**
  70. * Format a string into a controller class name.
  71. *
  72. * @param string $unformatted
  73. * @return string
  74. */
  75. public function formatControllerName($unformatted);
  76. /**
  77. * Format a string into an action method name.
  78. *
  79. * @param string $unformatted
  80. * @return string
  81. */
  82. public function formatActionName($unformatted);
  83. /**
  84. * Determine if a request is dispatchable
  85. *
  86. * @param Zend_Controller_Request_Abstract $request
  87. * @return boolean
  88. */
  89. public function isDispatchable(
  90. Zend_Controller_Request_Abstract $request
  91. );
  92. /**
  93. * Set a user parameter (via front controller, or for local use)
  94. *
  95. * @param string $name
  96. * @param mixed $value
  97. * @return Zend_Controller_Dispatcher_Interface
  98. */
  99. public function setParam($name, $value);
  100. /**
  101. * Set an array of user parameters
  102. *
  103. * @param array $params
  104. * @return Zend_Controller_Dispatcher_Interface
  105. */
  106. public function setParams(array $params);
  107. /**
  108. * Retrieve a single user parameter
  109. *
  110. * @param string $name
  111. * @return mixed
  112. */
  113. public function getParam($name);
  114. /**
  115. * Retrieve all user parameters
  116. *
  117. * @return array
  118. */
  119. public function getParams();
  120. /**
  121. * Clear the user parameter stack, or a single user parameter
  122. *
  123. * @param null|string|array single key or array of keys for
  124. * params to clear
  125. * @return Zend_Controller_Dispatcher_Interface
  126. */
  127. public function clearParams($name = null);
  128. /**
  129. * Set the response object to use, if any
  130. *
  131. * @param Zend_Controller_Response_Abstract|null $response
  132. * @return void
  133. */
  134. public function setResponse(
  135. Zend_Controller_Response_Abstract $response = null
  136. );
  137. /**
  138. * Retrieve the response object, if any
  139. *
  140. * @return Zend_Controller_Response_Abstract|null
  141. */
  142. public function getResponse();
  143. /**
  144. * Add a controller directory to the controller directory stack
  145. *
  146. * @param string $path
  147. * @param string $args
  148. * @return Zend_Controller_Dispatcher_Interface
  149. */
  150. public function addControllerDirectory($path, $args = null);
  151. /**
  152. * Set the directory (or directories) where controller files are
  153. * stored
  154. *
  155. * @param string|array $dir
  156. * @return Zend_Controller_Dispatcher_Interface
  157. */
  158. public function setControllerDirectory($path);
  159. /**
  160. * Return the currently set directory(ies) for controller file
  161. * lookup
  162. *
  163. * @return array
  164. */
  165. public function getControllerDirectory();
  166. /**
  167. * Dispatch a request to a (module/)controller/action.
  168. *
  169. * @param Zend_Controller_Request_Abstract $request
  170. * @param Zend_Controller_Response_Abstract $response
  171. * @return Zend_Controller_Request_Abstract|boolean
  172. */
  173. public function dispatch(
  174. Zend_Controller_Request_Abstract $request,
  175. Zend_Controller_Response_Abstract $response
  176. );
  177. /**
  178. * Whether or not a given module is valid
  179. *
  180. * @param string $module
  181. * @return boolean
  182. */
  183. public function isValidModule($module);
  184. /**
  185. * Retrieve the default module name
  186. *
  187. * @return string
  188. */
  189. public function getDefaultModule();
  190. /**
  191. * Retrieve the default controller name
  192. *
  193. * @return string
  194. */
  195. public function getDefaultControllerName();
  196. /**
  197. * Retrieve the default action
  198. *
  199. * @return string
  200. */
  201. public function getDefaultAction();
  202. }
  203. ]]>
  204. </programlisting>
  205. <para>
  206. 不过大多数情况下,只需要简单地扩展抽象类<code>Zend_Controller_Dispatcher_Abstract</code>,其中已经定义好了上面的大部分方法。或者扩展<code>Zend_Controller_Dispatcher_Standard</code>类,基于标准分发器来修改功能。
  207. </para>
  208. <para>
  209. 需要子类化分发器的可能原因包括:期望在动作控制器中使用不同的类和方法命名模式,或者期望使用不同的分发方式,比如分发到控制器目录下的动作文件,而不是控制器类的动作方法。
  210. </para>
  211. </sect2>
  212. </sect1>
  213. <!--
  214. vim:se ts=4 sw=4 et:
  215. -->