Zend_Controller-FrontController.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.front">
  4. <title>The Front Controller</title>
  5. <sect2 id="zend.controller.front.overview">
  6. <title>Overview</title>
  7. <para>
  8. <classname>Zend_Controller_Front</classname> implements a <ulink
  9. url="http://www.martinfowler.com/eaaCatalog/frontController.html">Front
  10. Controller pattern</ulink> used in <ulink
  11. url="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller
  12. (MVC)</ulink> applications. Its purpose is to initialize the
  13. request environment, route the incoming request, and then dispatch
  14. any discovered actions; it aggregates any responses and returns them
  15. when the process is complete.
  16. </para>
  17. <para>
  18. <classname>Zend_Controller_Front</classname> also implements the <ulink
  19. url="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton
  20. pattern</ulink>, meaning only a single instance of it may be available at
  21. any given time. This allows it to also act as a registry on which
  22. the other objects in the dispatch process may draw.
  23. </para>
  24. <para>
  25. <classname>Zend_Controller_Front</classname> registers a <link
  26. linkend="zend.controller.plugins">plugin broker</link> with
  27. itself, allowing various events it triggers to be observed by
  28. plugins. In most cases, this gives the developer the opportunity to
  29. tailor the dispatch process to the site without the need to extend
  30. the front controller to add functionality.
  31. </para>
  32. <para>
  33. At a bare minimum, the front controller needs one or more paths to
  34. directories containing <link linkend="zend.controller.action">action
  35. controllers</link> in order to do its work. A variety of methods
  36. may also be invoked to further tailor the front controller
  37. environment and that of its helper classes.
  38. </para>
  39. <note>
  40. <title>Default Behaviour</title>
  41. <para>
  42. By default, the front controller loads the <link
  43. linkend="zend.controller.plugins.standard.errorhandler">ErrorHandler</link>
  44. plugin, as well as the <link
  45. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>
  46. action helper plugin. These are to simplify error handling and
  47. view renderering in your controllers, respectively.
  48. </para>
  49. <para>
  50. To disable the <emphasis>ErrorHandler</emphasis>, perform the following
  51. at any point prior to calling <methodname>dispatch()</methodname>:
  52. </para>
  53. <programlisting language="php"><![CDATA[
  54. // Disable the ErrorHandler plugin:
  55. $front->setParam('noErrorHandler', true);
  56. ]]></programlisting>
  57. <para>
  58. To disable the <emphasis>ViewRenderer</emphasis>, do the following prior
  59. to calling <methodname>dispatch()</methodname>:
  60. </para>
  61. <programlisting language="php"><![CDATA[
  62. // Disable the ViewRenderer helper:
  63. $front->setParam('noViewRenderer', true);
  64. ]]></programlisting>
  65. </note>
  66. </sect2>
  67. <sect2 id="zend.controller.front.methods.primary">
  68. <title>Primary Methods</title>
  69. <para>
  70. The front controller has several accessors for setting up its
  71. environment. However, there are three primary methods key to the
  72. front controller's functionality:
  73. </para>
  74. <sect3 id="zend.controller.front.methods.primary.getinstance">
  75. <title>getInstance()</title>
  76. <para>
  77. <methodname>getInstance()</methodname> is used to retrieve a front
  78. controller instance. As the front controller implements a
  79. Singleton pattern, this is also the only means possible for
  80. instantiating a front controller object.
  81. </para>
  82. <programlisting language="php"><![CDATA[
  83. $front = Zend_Controller_Front::getInstance();
  84. ]]></programlisting>
  85. </sect3>
  86. <sect3 id="zend.controller.front.methods.primary.setcontrollerdirectory">
  87. <title>setControllerDirectory() and addControllerDirectory</title>
  88. <para>
  89. <methodname>setControllerDirectory()</methodname> is used to tell <link
  90. linkend="zend.controller.dispatcher">the dispatcher</link>
  91. where to look for <link
  92. linkend="zend.controller.action">action controller</link>
  93. class files. It accepts either a single path or an associative
  94. array of module and path pairs.
  95. </para>
  96. <para>
  97. As some examples:
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. // Set the default controller directory:
  101. $front->setControllerDirectory('../application/controllers');
  102. // Set several module directories at once:
  103. $front->setControllerDirectory(array(
  104. 'default' => '../application/controllers',
  105. 'blog' => '../modules/blog/controllers',
  106. 'news' => '../modules/news/controllers',
  107. ));
  108. // Add a 'foo' module directory:
  109. $front->addControllerDirectory('../modules/foo/controllers', 'foo');
  110. ]]></programlisting>
  111. <note>
  112. <para>
  113. If you use <methodname>addControllerDirectory()</methodname> without a
  114. module name, it will set the directory for the
  115. <emphasis>default</emphasis> module -- overwriting it if it already
  116. exists.
  117. </para>
  118. </note>
  119. <para>
  120. You can get the current settings for the controller directory
  121. using <methodname>getControllerDirectory()</methodname>; this will return an
  122. array of module and directory pairs.
  123. </para>
  124. </sect3>
  125. <sect3 id="zend.controller.front.methods.primary.addmoduledirectory">
  126. <title>addModuleDirectory() and getModuleDirectory()</title>
  127. <para>
  128. One aspect of the front controller is that you may <link
  129. linkend="zend.controller.modular">define a modular directory
  130. structure</link> for creating standalone components; these are
  131. called "modules".
  132. </para>
  133. <para>
  134. Each module should be in its own directory and mirror the
  135. directory structure of the default module -- i.e., it should
  136. have a <filename>/controllers/</filename> subdirectory at the minimum, and typically
  137. a <filename>/views/</filename> subdirectory and other application subdirectories.
  138. </para>
  139. <para>
  140. <methodname>addModuleDirectory()</methodname> allows you to pass the name of
  141. a directory containing one or more module directories. It then
  142. scans it and adds them as controller directories to the front
  143. controller.
  144. </para>
  145. <para>
  146. Later, if you want to determine the path to a particular module
  147. or the current module, you can call
  148. <methodname>getModuleDirectory()</methodname>, optionally passing a module
  149. name to get that specific module directory.
  150. </para>
  151. </sect3>
  152. <sect3 id="zend.controller.front.methods.primary.dispatch">
  153. <title>dispatch()</title>
  154. <para>
  155. <methodname>dispatch(Zend_Controller_Request_Abstract $request = null,
  156. Zend_Controller_Response_Abstract $response = null)</methodname>
  157. does the heavy work of the front controller. It may optionally
  158. take a <link linkend="zend.controller.request">request
  159. object</link> and/or a <link
  160. linkend="zend.controller.response">response object</link>,
  161. allowing the developer to pass in custom objects for each.
  162. </para>
  163. <para>
  164. If no request or response object are passed in,
  165. <methodname>dispatch()</methodname> will check for previously registered
  166. objects and use those or instantiate default versions to use in
  167. its process (in both cases, the <acronym>HTTP</acronym> flavor will be used as the
  168. default).
  169. </para>
  170. <para>
  171. Similarly, <methodname>dispatch()</methodname> checks for registered <link
  172. linkend="zend.controller.router">router</link> and <link
  173. linkend="zend.controller.dispatcher">dispatcher</link>
  174. objects, instantiating the default versions of each if none is
  175. found.
  176. </para>
  177. <para>
  178. The dispatch process has three distinct events:
  179. </para>
  180. <itemizedlist>
  181. <listitem><para>Routing</para></listitem>
  182. <listitem><para>Dispatching</para></listitem>
  183. <listitem><para>Response</para></listitem>
  184. </itemizedlist>
  185. <para>
  186. Routing takes place exactly once, using the values in the
  187. request object when <methodname>dispatch()</methodname> is called.
  188. Dispatching takes place in a loop; a request may either indicate
  189. multiple actions to dispatch, or the controller or a plugin may
  190. reset the request object to force additional actions to
  191. dispatch. When all is done, the front controller returns a
  192. response.
  193. </para>
  194. </sect3>
  195. <sect3 id="zend.controller.front.methods.primary.run">
  196. <title>run()</title>
  197. <para>
  198. <methodname>Zend_Controller_Front::run($path)</methodname> is a static
  199. method taking simply a path to a directory containing
  200. controllers. It fetches a front controller instance (via
  201. <link
  202. linkend="zend.controller.front.methods.primary.getinstance">getInstance()</link>),
  203. registers the path provided via <link
  204. linkend="zend.controller.front.methods.primary.setcontrollerdirectory">setControllerDirectory()</link>,
  205. and finally <link
  206. linkend="zend.controller.front.methods.primary.dispatch">dispatches</link>.
  207. </para>
  208. <para>
  209. Basically, <methodname>run()</methodname> is a convenience method that can
  210. be used for site setups that do not require customization of the
  211. front controller environment.
  212. </para>
  213. <programlisting language="php"><![CDATA[
  214. // Instantiate front controller, set controller directory, and dispatch in one
  215. // easy step:
  216. Zend_Controller_Front::run('../application/controllers');
  217. ]]></programlisting>
  218. </sect3>
  219. </sect2>
  220. <sect2 id="zend.controller.front.methods.environment">
  221. <title>Environmental Accessor Methods</title>
  222. <para>
  223. In addition to the methods listed above, there are a number of
  224. accessor methods that can be used to affect the front controller
  225. environment -- and thus the environment of the classes to which the
  226. front controller delegates.
  227. </para>
  228. <itemizedlist>
  229. <listitem>
  230. <para>
  231. <methodname>resetInstance()</methodname> can be used to clear all
  232. current settings. Its primary purpose is for testing, but it
  233. can also be used for instances where you wish to chain
  234. together multiple front controllers.
  235. </para>
  236. </listitem>
  237. <listitem>
  238. <para>
  239. <methodname>setDefaultControllerName()</methodname> and
  240. <methodname>getDefaultControllerName()</methodname> let you
  241. specify a different name to use for the default controller
  242. ('index' is used otherwise) and retrieve the current value.
  243. They proxy to <link linkend="zend.controller.dispatcher">the dispatcher</link>.
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <methodname>setDefaultAction()</methodname> and
  249. <methodname>getDefaultAction()</methodname> let you specify a
  250. different name to use for the default action ('index' is
  251. used otherwise) and retrieve the current value. They proxy
  252. to <link linkend="zend.controller.dispatcher">the dispatcher</link>.
  253. </para>
  254. </listitem>
  255. <listitem>
  256. <para>
  257. <methodname>setRequest()</methodname> and
  258. <methodname>getRequest()</methodname> let you specify <link
  259. linkend="zend.controller.request">the request</link>
  260. class or object to use during the dispatch process and to
  261. retrieve the current object. When setting the request
  262. object, you may pass in a request class name, in which case
  263. the method will load the class file and instantiate it.
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <methodname>setRouter()</methodname>
  269. <methodname>getRouter()</methodname> let you specify <link
  270. linkend="zend.controller.router">the router</link>
  271. class or object to use during the dispatch process and to
  272. retrieve the current object. When setting the router
  273. object, you may pass in a router class name, in which case
  274. the method will load the class file and instantiate it.
  275. </para>
  276. <para>
  277. When retrieving the router object, it first checks to see if
  278. one is present, and if not, instantiates the default router
  279. (rewrite router).
  280. </para>
  281. </listitem>
  282. <listitem>
  283. <para>
  284. <methodname>setBaseUrl()</methodname> and
  285. <methodname>getBaseUrl()</methodname> let you specify <link
  286. linkend="zend.controller.request.http.baseurl">the base
  287. <acronym>URL</acronym></link> to strip when routing requests and to
  288. retrieve the current value. The value is provided to the
  289. request object just prior to routing.
  290. </para>
  291. <note>
  292. <title>Fully-Qualified URL is not supported</title>
  293. <para>
  294. Passing a fully-qualified URL (ie: http://example.com/) to the
  295. <methodname>setBaseUrl</methodname> method is not supported, and
  296. will cause issues when using the URL view helper. See ticket
  297. <ulink url="http://framework.zend.com/issues/browse/ZF-10923">
  298. ZF-10923
  299. </ulink> for more details.
  300. </para>
  301. </note>
  302. </listitem>
  303. <listitem>
  304. <para>
  305. <methodname>setDispatcher()</methodname> and
  306. <methodname>getDispatcher()</methodname> let you specify <link
  307. linkend="zend.controller.dispatcher">the
  308. dispatcher</link> class or object to use during the
  309. dispatch process and retrieve the current object. When
  310. setting the dispatcher object, you may pass in a dispatcher
  311. class name, in which case the method will load the class
  312. file and instantiate it.
  313. </para>
  314. <para>
  315. When retrieving the dispatcher object, it first checks to see if
  316. one is present, and if not, instantiates the default
  317. dispatcher.
  318. </para>
  319. </listitem>
  320. <listitem>
  321. <para>
  322. <methodname>setResponse()</methodname> and
  323. <methodname>getResponse()</methodname> let you specify <link
  324. linkend="zend.controller.response">the response</link>
  325. class or object to use during the dispatch process and to
  326. retrieve the current object. When setting the response
  327. object, you may pass in a response class name, in which case
  328. the method will load the class file and instantiate it.
  329. </para>
  330. </listitem>
  331. <listitem>
  332. <para>
  333. <methodname>registerPlugin(Zend_Controller_Plugin_Abstract $plugin,
  334. $stackIndex = null)</methodname> allows you to register <link
  335. linkend="zend.controller.plugins">plugin objects</link>.
  336. By setting the optional <varname>$stackIndex</varname>, you can
  337. control the order in which plugins will execute.
  338. </para>
  339. </listitem>
  340. <listitem>
  341. <para>
  342. <methodname>unregisterPlugin($plugin)</methodname> let you
  343. unregister <link
  344. linkend="zend.controller.plugins">plugin objects</link>.
  345. <varname>$plugin</varname> may be either a plugin object or a
  346. string denoting the class of plugin to unregister.
  347. </para>
  348. </listitem>
  349. <listitem>
  350. <para>
  351. <methodname>throwExceptions($flag)</methodname> is used to turn on/off
  352. the ability to throw exceptions during the dispatch process.
  353. By default, exceptions are caught and placed in the <link
  354. linkend="zend.controller.response">response
  355. object</link>; turning on <methodname>throwExceptions()</methodname>
  356. will override this behaviour.
  357. </para>
  358. <para>
  359. For more information, read <link linkend="zend.controller.exceptions">MVC
  360. Exceptions</link>.
  361. </para>
  362. </listitem>
  363. <listitem>
  364. <para>
  365. <methodname>returnResponse($flag)</methodname> is used to tell the front
  366. controller whether to return the response
  367. (<constant>TRUE</constant>) from <methodname>dispatch()</methodname>, or if the
  368. response should be automatically emitted
  369. (<constant>FALSE</constant>). By default, the response is
  370. automatically emitted (by calling
  371. <methodname>Zend_Controller_Response_Abstract::sendResponse()</methodname>);
  372. turning on <methodname>returnResponse()</methodname> will override this
  373. behaviour.
  374. </para>
  375. <para>
  376. Reasons to return the response include a desire to check for
  377. exceptions prior to emitting the response, needing to log
  378. various aspects of the response (such as headers), etc.
  379. </para>
  380. </listitem>
  381. </itemizedlist>
  382. </sect2>
  383. <sect2 id="zend.controller.front.methods.params">
  384. <title>Front Controller Parameters</title>
  385. <para>
  386. In the introduction, we indicated that the front controller also
  387. acts as a registry for the various controller components. It does so
  388. through a family of "param" methods. These methods allow you to
  389. register arbitrary data -- objects and variables -- with the front
  390. controller to be retrieved at any time in the dispatch chain. These
  391. values are passed on to the router, dispatcher, and action
  392. controllers. The methods include:
  393. </para>
  394. <itemizedlist>
  395. <listitem>
  396. <para>
  397. <methodname>setParam($name, $value)</methodname> allows you to set a
  398. single parameter of <varname>$name</varname> with value
  399. <varname>$value</varname>.
  400. </para>
  401. </listitem>
  402. <listitem>
  403. <para>
  404. <methodname>setParams(array $params)</methodname> allows you to set
  405. multiple parameters at once using an associative array.
  406. </para>
  407. </listitem>
  408. <listitem>
  409. <para>
  410. <methodname>getParam($name)</methodname> allows you to retrieve a single
  411. parameter at a time, using <varname>$name</varname> as the
  412. identifier.
  413. </para>
  414. </listitem>
  415. <listitem>
  416. <para>
  417. <methodname>getParams()</methodname> allows you to retrieve the entire
  418. list of parameters at once.
  419. </para>
  420. </listitem>
  421. <listitem>
  422. <para>
  423. <methodname>clearParams()</methodname> allows you to clear a single
  424. parameter (by passing a string identifier), multiple named
  425. parameters (by passing an array of string identifiers), or the
  426. entire parameter stack (by passing nothing).
  427. </para>
  428. </listitem>
  429. </itemizedlist>
  430. <para>
  431. There are several pre-defined parameters that may be set that have
  432. specific uses in the dispatch chain:
  433. </para>
  434. <itemizedlist>
  435. <listitem>
  436. <para>
  437. <emphasis>useDefaultControllerAlways</emphasis> is used to hint to
  438. <link linkend="zend.controller.dispatcher">the
  439. dispatcher</link> to use the default controller in the
  440. default module for any request that is not dispatchable
  441. (i.e., the module, controller, and/or action do not exist).
  442. By default, this is off.
  443. </para>
  444. <para>
  445. See <link linkend="zend.controller.exceptions.internal">MVC Exceptions
  446. You May Encounter</link>
  447. for more detailed information on using this setting.
  448. </para>
  449. </listitem>
  450. <listitem>
  451. <para>
  452. <emphasis>disableOutputBuffering</emphasis> is used to hint to <link
  453. linkend="zend.controller.dispatcher">the
  454. dispatcher</link> that it should not use output
  455. buffering to capture output generated by action controllers.
  456. By default, the dispatcher captures any output and appends
  457. it to the response object body content.
  458. </para>
  459. </listitem>
  460. <listitem>
  461. <para>
  462. <emphasis>noViewRenderer</emphasis> is used to disable the <link
  463. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>.
  464. Set this parameter to <constant>TRUE</constant> to disable it.
  465. </para>
  466. </listitem>
  467. <listitem>
  468. <para>
  469. <emphasis>noErrorHandler</emphasis> is used to disable the <link
  470. linkend="zend.controller.plugins.standard.errorhandler">Error
  471. Handler plugin</link>. Set this parameter to <constant>TRUE</constant> to
  472. disable it.
  473. </para>
  474. </listitem>
  475. </itemizedlist>
  476. </sect2>
  477. <sect2 id="zend.controller.front.subclassing">
  478. <title>Extending the Front Controller</title>
  479. <para>
  480. To extend the Front Controller, at the very minimum you will need
  481. to override the <methodname>getInstance()</methodname> method:
  482. </para>
  483. <programlisting language="php"><![CDATA[
  484. class My_Controller_Front extends Zend_Controller_Front
  485. {
  486. public static function getInstance()
  487. {
  488. if (null === self::$_instance) {
  489. self::$_instance = new self();
  490. }
  491. return self::$_instance;
  492. }
  493. }
  494. ]]></programlisting>
  495. <para>
  496. Overriding the <methodname>getInstance()</methodname> method ensures that
  497. subsequent calls to
  498. <methodname>Zend_Controller_Front::getInstance()</methodname> will return an
  499. instance of your new subclass instead of a
  500. <classname>Zend_Controller_Front</classname> instance -- this is particularly
  501. useful for some of the alternate routers and view helpers.
  502. </para>
  503. <para>
  504. Typically, you will not need to subclass the front controller unless
  505. you need to add new functionality (for instance, a plugin
  506. autoloader, or a way to specify action helper paths). Some points
  507. where you may want to alter behaviour may include modifying how
  508. controller directories are stored, or what default router or
  509. dispatcher are used.
  510. </para>
  511. </sect2>
  512. </sect1>
  513. <!--
  514. vim:se ts=4 sw=4 et:
  515. -->