2
0

Zend_Controller-Dispatcher.xml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.dispatcher">
  4. <title>The Dispatcher</title>
  5. <sect2 id="zend.controller.dispatcher.overview">
  6. <title>Overview</title>
  7. <para>
  8. Dispatching is the process of taking the request object,
  9. <classname>Zend_Controller_Request_Abstract</classname>, extracting the module
  10. name, controller name, action name, and optional parameters
  11. contained in it, and then instantiating a controller and calling an
  12. action of that controller. If any of the module, controller, or
  13. action are not found, it will use default values for them.
  14. <classname>Zend_Controller_Dispatcher_Standard</classname> specifies
  15. <code>index</code> for each of the controller and action defaults
  16. and <code>default</code> for the module default value, but allows
  17. the developer to change the default values for each using the
  18. <code>setDefaultController()</code>,
  19. <code>setDefaultAction()</code>, and <code>setDefaultModule()</code>
  20. methods, respectively.
  21. </para>
  22. <note>
  23. <title>Default Module</title>
  24. <para>
  25. When creating modular applications, you may find that you want
  26. your default module namespaced as well (the default
  27. configuration is that the default module is
  28. <emphasis>not</emphasis> namespaced). As of 1.5.0, you can now
  29. do so by specifying the <code>prefixDefaultModule</code> as true
  30. in either the front controller or your dispatcher:
  31. </para>
  32. <programlisting language="php"><![CDATA[
  33. // In your front controller:
  34. $front->setParam('prefixDefaultModule', true);
  35. // In your dispatcher:
  36. $dispatcher->setParam('prefixDefaultModule', true);
  37. ]]></programlisting>
  38. <para>
  39. This allows you to re-purpose an existing module to be the
  40. default module for an application.
  41. </para>
  42. </note>
  43. <para>
  44. Dispatching happens in a loop in the front controller. Before
  45. dispatching occurs, the front controller routes the request to find
  46. user specified values for the module, controller, action, and optional
  47. parameters. It then enters a dispatch loop, dispatching the request.
  48. </para>
  49. <para>
  50. At the beginning of each iteration, it sets a flag in the request
  51. object indicating that the action has been dispatched. If an action
  52. or pre/postDispatch plugin resets that flag, the dispatch loop will
  53. continue and attempt to dispatch the new request. By changing the
  54. controller and/or action in the request and resetting the dispatched
  55. flag, the developer may define a chain of requests to perform.
  56. </para>
  57. <para>
  58. The action controller method that controls such dispatching is
  59. <code>_forward()</code>; call this method from any of the
  60. pre/postDispatch() or action methods, providing an action, controller,
  61. module, and optionally any additional parameters you may wish to
  62. send to the new action:
  63. </para>
  64. <programlisting language="php"><![CDATA[
  65. public function fooAction()
  66. {
  67. // forward to another action in the current controller and module:
  68. $this->_forward('bar', null, null, array('baz' => 'bogus'));
  69. }
  70. public function barAction()
  71. {
  72. // forward to an action in another controller:
  73. // FooController::bazAction(),
  74. // in the current module:
  75. $this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
  76. }
  77. public function bazAction()
  78. {
  79. // forward to an action in another controller in another module,
  80. // Foo_BarController::bazAction():
  81. $this->_forward('baz', 'bar', 'foo', array('baz' => 'bogus'));
  82. }
  83. ]]></programlisting>
  84. </sect2>
  85. <sect2 id="zend.controller.dispatcher.subclassing">
  86. <title>Subclassing the Dispatcher</title>
  87. <para>
  88. <classname>Zend_Controller_Front</classname> will first call the router to
  89. determine the first action in the request. It then enters a dispatch
  90. loop, which calls on the dispatcher to dispatch the action.
  91. </para>
  92. <para>
  93. The dispatcher needs a variety of data in order to do its work - it
  94. needs to know how to format controller and action names, where to
  95. look for controller class files, whether or not a provided module
  96. name is valid, and an API for determining if a given request is even
  97. dispatchable based on the other information available.
  98. </para>
  99. <para>
  100. <classname>Zend_Controller_Dispatcher_Interface</classname> defines the
  101. following methods as required for any dispatcher implementation:
  102. </para>
  103. <programlisting language="php"><![CDATA[
  104. interface Zend_Controller_Dispatcher_Interface
  105. {
  106. /**
  107. * Format a string into a controller class name.
  108. *
  109. * @param string $unformatted
  110. * @return string
  111. */
  112. public function formatControllerName($unformatted);
  113. /**
  114. * Format a string into an action method name.
  115. *
  116. * @param string $unformatted
  117. * @return string
  118. */
  119. public function formatActionName($unformatted);
  120. /**
  121. * Determine if a request is dispatchable
  122. *
  123. * @param Zend_Controller_Request_Abstract $request
  124. * @return boolean
  125. */
  126. public function isDispatchable(
  127. Zend_Controller_Request_Abstract $request
  128. );
  129. /**
  130. * Set a user parameter (via front controller, or for local use)
  131. *
  132. * @param string $name
  133. * @param mixed $value
  134. * @return Zend_Controller_Dispatcher_Interface
  135. */
  136. public function setParam($name, $value);
  137. /**
  138. * Set an array of user parameters
  139. *
  140. * @param array $params
  141. * @return Zend_Controller_Dispatcher_Interface
  142. */
  143. public function setParams(array $params);
  144. /**
  145. * Retrieve a single user parameter
  146. *
  147. * @param string $name
  148. * @return mixed
  149. */
  150. public function getParam($name);
  151. /**
  152. * Retrieve all user parameters
  153. *
  154. * @return array
  155. */
  156. public function getParams();
  157. /**
  158. * Clear the user parameter stack, or a single user parameter
  159. *
  160. * @param null|string|array single key or array of keys for
  161. * params to clear
  162. * @return Zend_Controller_Dispatcher_Interface
  163. */
  164. public function clearParams($name = null);
  165. /**
  166. * Set the response object to use, if any
  167. *
  168. * @param Zend_Controller_Response_Abstract|null $response
  169. * @return void
  170. */
  171. public function setResponse(
  172. Zend_Controller_Response_Abstract $response = null
  173. );
  174. /**
  175. * Retrieve the response object, if any
  176. *
  177. * @return Zend_Controller_Response_Abstract|null
  178. */
  179. public function getResponse();
  180. /**
  181. * Add a controller directory to the controller directory stack
  182. *
  183. * @param string $path
  184. * @param string $args
  185. * @return Zend_Controller_Dispatcher_Interface
  186. */
  187. public function addControllerDirectory($path, $args = null);
  188. /**
  189. * Set the directory (or directories) where controller files are
  190. * stored
  191. *
  192. * @param string|array $dir
  193. * @return Zend_Controller_Dispatcher_Interface
  194. */
  195. public function setControllerDirectory($path);
  196. /**
  197. * Return the currently set directory(ies) for controller file
  198. * lookup
  199. *
  200. * @return array
  201. */
  202. public function getControllerDirectory();
  203. /**
  204. * Dispatch a request to a (module/)controller/action.
  205. *
  206. * @param Zend_Controller_Request_Abstract $request
  207. * @param Zend_Controller_Response_Abstract $response
  208. * @return Zend_Controller_Request_Abstract|boolean
  209. */
  210. public function dispatch(
  211. Zend_Controller_Request_Abstract $request,
  212. Zend_Controller_Response_Abstract $response
  213. );
  214. /**
  215. * Whether or not a given module is valid
  216. *
  217. * @param string $module
  218. * @return boolean
  219. */
  220. public function isValidModule($module);
  221. /**
  222. * Retrieve the default module name
  223. *
  224. * @return string
  225. */
  226. public function getDefaultModule();
  227. /**
  228. * Retrieve the default controller name
  229. *
  230. * @return string
  231. */
  232. public function getDefaultControllerName();
  233. /**
  234. * Retrieve the default action
  235. *
  236. * @return string
  237. */
  238. public function getDefaultAction();
  239. }
  240. ]]></programlisting>
  241. <para>
  242. In most cases, however, you should simply extend the abstract class
  243. <classname>Zend_Controller_Dispatcher_Abstract</classname>, in which each of
  244. these have already been defined, or
  245. <classname>Zend_Controller_Dispatcher_Standard</classname> to modify
  246. functionality of the standard dispatcher.
  247. </para>
  248. <para>
  249. Possible reasons to subclass the dispatcher include a desire to
  250. use a different class or method naming schema in your action
  251. controllers, or a desire to use a different dispatching paradigm
  252. such as dispatching to action files under controller directories
  253. (instead of dispatching to class methods).
  254. </para>
  255. </sect2>
  256. </sect1>
  257. <!--
  258. vim:se ts=4 sw=4 et:
  259. -->