Zend_Controller-Dispatcher.xml 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. <emphasis>index</emphasis> for each of the controller and action defaults
  16. and <emphasis>default</emphasis> for the module default value, but allows
  17. the developer to change the default values for each using the
  18. <methodname>setDefaultController()</methodname>,
  19. <methodname>setDefaultAction()</methodname>, and
  20. <methodname>setDefaultModule()</methodname> 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 <property>prefixDefaultModule</property> as
  30. <constant>TRUE</constant> 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 or 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. <methodname>_forward()</methodname>; call this method from any of the
  60. <methodname>preDispatch()</methodname>, <methodname>postDispatch()</methodname> or
  61. action methods, providing an action, controller,
  62. module, and optionally any additional parameters you may wish to
  63. send to the new action:
  64. </para>
  65. <programlisting language="php"><![CDATA[
  66. public function fooAction()
  67. {
  68. // forward to another action in the current controller and module:
  69. $this->_forward('bar', null, null, array('baz' => 'bogus'));
  70. }
  71. public function barAction()
  72. {
  73. // forward to an action in another controller:
  74. // FooController::bazAction(),
  75. // in the current module:
  76. $this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
  77. }
  78. public function bazAction()
  79. {
  80. // forward to an action in another controller in another module,
  81. // Foo_BarController::bazAction():
  82. $this->_forward('baz', 'bar', 'foo', array('baz' => 'bogus'));
  83. }
  84. ]]></programlisting>
  85. </sect2>
  86. <sect2 id="zend.controller.dispatcher.subclassing">
  87. <title>Subclassing the Dispatcher</title>
  88. <para>
  89. <classname>Zend_Controller_Front</classname> will first call the router to
  90. determine the first action in the request. It then enters a dispatch
  91. loop, which calls on the dispatcher to dispatch the action.
  92. </para>
  93. <para>
  94. The dispatcher needs a variety of data in order to do its work - it
  95. needs to know how to format controller and action names, where to
  96. look for controller class files, whether or not a provided module
  97. name is valid, and an <acronym>API</acronym> for determining if a given request is even
  98. dispatchable based on the other information available.
  99. </para>
  100. <para>
  101. <classname>Zend_Controller_Dispatcher_Interface</classname> defines the
  102. following methods as required for any dispatcher implementation:
  103. </para>
  104. <programlisting language="php"><![CDATA[
  105. interface Zend_Controller_Dispatcher_Interface
  106. {
  107. /**
  108. * Format a string into a controller class name.
  109. *
  110. * @param string $unformatted
  111. * @return string
  112. */
  113. public function formatControllerName($unformatted);
  114. /**
  115. * Format a string into an action method name.
  116. *
  117. * @param string $unformatted
  118. * @return string
  119. */
  120. public function formatActionName($unformatted);
  121. /**
  122. * Determine if a request is dispatchable
  123. *
  124. * @param Zend_Controller_Request_Abstract $request
  125. * @return boolean
  126. */
  127. public function isDispatchable(
  128. Zend_Controller_Request_Abstract $request
  129. );
  130. /**
  131. * Set a user parameter (via front controller, or for local use)
  132. *
  133. * @param string $name
  134. * @param mixed $value
  135. * @return Zend_Controller_Dispatcher_Interface
  136. */
  137. public function setParam($name, $value);
  138. /**
  139. * Set an array of user parameters
  140. *
  141. * @param array $params
  142. * @return Zend_Controller_Dispatcher_Interface
  143. */
  144. public function setParams(array $params);
  145. /**
  146. * Retrieve a single user parameter
  147. *
  148. * @param string $name
  149. * @return mixed
  150. */
  151. public function getParam($name);
  152. /**
  153. * Retrieve all user parameters
  154. *
  155. * @return array
  156. */
  157. public function getParams();
  158. /**
  159. * Clear the user parameter stack, or a single user parameter
  160. *
  161. * @param null|string|array single key or array of keys for
  162. * params to clear
  163. * @return Zend_Controller_Dispatcher_Interface
  164. */
  165. public function clearParams($name = null);
  166. /**
  167. * Set the response object to use, if any
  168. *
  169. * @param Zend_Controller_Response_Abstract|null $response
  170. * @return void
  171. */
  172. public function setResponse(
  173. Zend_Controller_Response_Abstract $response = null
  174. );
  175. /**
  176. * Retrieve the response object, if any
  177. *
  178. * @return Zend_Controller_Response_Abstract|null
  179. */
  180. public function getResponse();
  181. /**
  182. * Add a controller directory to the controller directory stack
  183. *
  184. * @param string $path
  185. * @param string $args
  186. * @return Zend_Controller_Dispatcher_Interface
  187. */
  188. public function addControllerDirectory($path, $args = null);
  189. /**
  190. * Set the directory (or directories) where controller files are
  191. * stored
  192. *
  193. * @param string|array $dir
  194. * @return Zend_Controller_Dispatcher_Interface
  195. */
  196. public function setControllerDirectory($path);
  197. /**
  198. * Return the currently set directory(ies) for controller file
  199. * lookup
  200. *
  201. * @return array
  202. */
  203. public function getControllerDirectory();
  204. /**
  205. * Dispatch a request to a (module/)controller/action.
  206. *
  207. * @param Zend_Controller_Request_Abstract $request
  208. * @param Zend_Controller_Response_Abstract $response
  209. * @return Zend_Controller_Request_Abstract|boolean
  210. */
  211. public function dispatch(
  212. Zend_Controller_Request_Abstract $request,
  213. Zend_Controller_Response_Abstract $response
  214. );
  215. /**
  216. * Whether or not a given module is valid
  217. *
  218. * @param string $module
  219. * @return boolean
  220. */
  221. public function isValidModule($module);
  222. /**
  223. * Retrieve the default module name
  224. *
  225. * @return string
  226. */
  227. public function getDefaultModule();
  228. /**
  229. * Retrieve the default controller name
  230. *
  231. * @return string
  232. */
  233. public function getDefaultControllerName();
  234. /**
  235. * Retrieve the default action
  236. *
  237. * @return string
  238. */
  239. public function getDefaultAction();
  240. }
  241. ]]></programlisting>
  242. <para>
  243. In most cases, however, you should simply extend the abstract class
  244. <classname>Zend_Controller_Dispatcher_Abstract</classname>, in which each of
  245. these have already been defined, or
  246. <classname>Zend_Controller_Dispatcher_Standard</classname> to modify
  247. functionality of the standard dispatcher.
  248. </para>
  249. <para>
  250. Possible reasons to subclass the dispatcher include a desire to
  251. use a different class or method naming schema in your action
  252. controllers, or a desire to use a different dispatching paradigm
  253. such as dispatching to action files under controller directories
  254. (instead of dispatching to class methods).
  255. </para>
  256. </sect2>
  257. </sect1>
  258. <!--
  259. vim:se ts=4 sw=4 et:
  260. -->