2
0

Zend_Controller-FrontController.xml 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 <code>ErrorHandler</code>, perform the following
  51. at any point prior to calling <code>dispatch()</code>:
  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 <code>ViewRenderer</code>, do the following prior
  59. to calling <code>dispatch()</code>:
  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. <code>getInstance()</code> 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. <code>setControllerDirectory()</code> 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/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 <code>addControllerDirectory()</code> without a
  114. module name, it will set the directory for the
  115. <code>default</code> 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 <code>getControllerDirectory()</code>; this will return an
  122. array of module/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 "controllers" subdirectory at the minimum, and typically
  137. a "views" subdirectory and other application subdirectories.
  138. </para>
  139. <para>
  140. <code>addModuleDirectory()</code> 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. <code>getModuleDirectory()</code>, 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. <code>dispatch(Zend_Controller_Request_Abstract $request = null,
  156. Zend_Controller_Response_Abstract $response = null)</code>
  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. <code>dispatch()</code> will check for previously registered
  166. objects and use those or instantiate default versions to use in
  167. its process (in both cases, the HTTP flavor will be used as the
  168. default).
  169. </para>
  170. <para>
  171. Similarly, <code>dispatch()</code> 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 <code>dispatch()</code> 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. <classname>Zend_Controller_Front::run($path)</classname> 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, <code>run()</code> 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. <code>resetInstance()</code> 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. <code>(set|get)DefaultControllerName()</code> let you
  240. specify a different name to use for the default controller
  241. ('index' is used otherwise) and retrieve the current value.
  242. They proxy to <link
  243. linkend="zend.controller.dispatcher">the
  244. dispatcher</link>.
  245. </para>
  246. </listitem>
  247. <listitem>
  248. <para>
  249. <code>(set|get)DefaultAction()</code> 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
  253. dispatcher</link>.
  254. </para>
  255. </listitem>
  256. <listitem>
  257. <para>
  258. <code>(set|get)Request()</code> 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. <code>(set|get)Router()</code> let you specify <link
  269. linkend="zend.controller.router">the router</link>
  270. class or object to use during the dispatch process and to
  271. retrieve the current object. When setting the router
  272. object, you may pass in a router class name, in which case
  273. the method will load the class file and instantiate it.
  274. </para>
  275. <para>
  276. When retrieving the router object, it first checks to see if
  277. one is present, and if not, instantiates the default router
  278. (rewrite router).
  279. </para>
  280. </listitem>
  281. <listitem>
  282. <para>
  283. <code>(set|get)BaseUrl()</code> let you specify <link
  284. linkend="zend.controller.request.http.baseurl">the base
  285. URL</link> to strip when routing requests and to
  286. retrieve the current value. The value is provided to the
  287. request object just prior to routing.
  288. </para>
  289. </listitem>
  290. <listitem>
  291. <para>
  292. <code>(set|get)Dispatcher()</code> let you specify <link
  293. linkend="zend.controller.dispatcher">the
  294. dispatcher</link> class or object to use during the
  295. dispatch process and retrieve the current object. When
  296. setting the dispatcher object, you may pass in a dispatcher
  297. class name, in which case the method will load the class
  298. file and instantiate it.
  299. </para>
  300. <para>
  301. When retrieving the dispatcher object, it first checks to see if
  302. one is present, and if not, instantiates the default
  303. dispatcher.
  304. </para>
  305. </listitem>
  306. <listitem>
  307. <para>
  308. <code>(set|get)Response()</code> let you specify <link
  309. linkend="zend.controller.response">the response</link>
  310. class or object to use during the dispatch process and to
  311. retrieve the current object. When setting the response
  312. object, you may pass in a response class name, in which case
  313. the method will load the class file and instantiate it.
  314. </para>
  315. </listitem>
  316. <listitem>
  317. <para>
  318. <code>registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex =
  319. null)</code> allows you to register <link
  320. linkend="zend.controller.plugins">plugin objects</link>.
  321. By setting the optional <varname>$stackIndex</varname>, you can
  322. control the order in which plugins will execute.
  323. </para>
  324. </listitem>
  325. <listitem>
  326. <para>
  327. <code>unregisterPlugin($plugin)</code> let you
  328. unregister <link
  329. linkend="zend.controller.plugins">plugin objects</link>.
  330. <varname>$plugin</varname> may be either a plugin object or a
  331. string denoting the class of plugin to unregister.
  332. </para>
  333. </listitem>
  334. <listitem>
  335. <para>
  336. <code>throwExceptions($flag)</code> is used to turn on/off
  337. the ability to throw exceptions during the dispatch process.
  338. By default, exceptions are caught and placed in the <link
  339. linkend="zend.controller.response">response
  340. object</link>; turning on <code>throwExceptions()</code>
  341. will override this behaviour.
  342. </para>
  343. <para>
  344. For more information, read <xref
  345. linkend="zend.controller.exceptions" />.
  346. </para>
  347. </listitem>
  348. <listitem>
  349. <para>
  350. <code>returnResponse($flag)</code> is used to tell the front
  351. controller whether to return the response
  352. (<constant>TRUE</constant>) from <code>dispatch()</code>, or if the
  353. response should be automatically emitted
  354. (<constant>FALSE</constant>). By default, the response is
  355. automatically emitted (by calling
  356. <classname>Zend_Controller_Response_Abstract::sendResponse()</classname>);
  357. turning on <code>returnResponse()</code> will override this
  358. behaviour.
  359. </para>
  360. <para>
  361. Reasons to return the response include a desire to check for
  362. exceptions prior to emitting the response, needing to log
  363. various aspects of the response (such as headers), etc.
  364. </para>
  365. </listitem>
  366. </itemizedlist>
  367. </sect2>
  368. <sect2 id="zend.controller.front.methods.params">
  369. <title>Front Controller Parameters</title>
  370. <para>
  371. In the introduction, we indicated that the front controller also
  372. acts as a registry for the various controller components. It does so
  373. through a family of "param" methods. These methods allow you to
  374. register arbitrary data -- objects and variables -- with the front
  375. controller to be retrieved at any time in the dispatch chain. These
  376. values are passed on to the router, dispatcher, and action
  377. controllers. The methods include:
  378. </para>
  379. <itemizedlist>
  380. <listitem>
  381. <para>
  382. <code>setParam($name, $value)</code> allows you to set a
  383. single parameter of <varname>$name</varname> with value
  384. <varname>$value</varname>.
  385. </para>
  386. </listitem>
  387. <listitem>
  388. <para>
  389. <code>setParams(array $params)</code> allows you to set
  390. multiple parameters at once using an associative array.
  391. </para>
  392. </listitem>
  393. <listitem>
  394. <para>
  395. <code>getParam($name)</code> allows you to retrieve a single
  396. parameter at a time, using <varname>$name</varname> as the
  397. identifier.
  398. </para>
  399. </listitem>
  400. <listitem>
  401. <para>
  402. <code>getParams()</code> allows you to retrieve the entire
  403. list of parameters at once.
  404. </para>
  405. </listitem>
  406. <listitem>
  407. <para>
  408. <code>clearParams()</code> allows you to clear a single
  409. parameter (by passing a string identifier), multiple named
  410. parameters (by passing an array of string identifiers), or the
  411. entire parameter stack (by passing nothing).
  412. </para>
  413. </listitem>
  414. </itemizedlist>
  415. <para>
  416. There are several pre-defined parameters that may be set that have
  417. specific uses in the dispatch chain:
  418. </para>
  419. <itemizedlist>
  420. <listitem>
  421. <para>
  422. <code>useDefaultControllerAlways</code> is used to hint to
  423. <link linkend="zend.controller.dispatcher">the
  424. dispatcher</link> to use the default controller in the
  425. default module for any request that is not dispatchable
  426. (i.e., the module, controller, and/or action do not exist).
  427. By default, this is off.
  428. </para>
  429. <para>
  430. See <xref linkend="zend.controller.exceptions.internal" />
  431. for more detailed information on using this setting.
  432. </para>
  433. </listitem>
  434. <listitem>
  435. <para>
  436. <code>disableOutputBuffering</code> is used to hint to <link
  437. linkend="zend.controller.dispatcher">the
  438. dispatcher</link> that it should not use output
  439. buffering to capture output generated by action controllers.
  440. By default, the dispatcher captures any output and appends
  441. it to the response object body content.
  442. </para>
  443. </listitem>
  444. <listitem>
  445. <para>
  446. <code>noViewRenderer</code> is used to disable the <link
  447. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>.
  448. Set this parameter to true to disable it.
  449. </para>
  450. </listitem>
  451. <listitem>
  452. <para>
  453. <code>noErrorHandler</code> is used to disable the <link
  454. linkend="zend.controller.plugins.standard.errorhandler">Error
  455. Handler plugin</link>. Set this parameter to true to
  456. disable it.
  457. </para>
  458. </listitem>
  459. </itemizedlist>
  460. </sect2>
  461. <sect2 id="zend.controller.front.subclassing">
  462. <title>Extending the Front Controller</title>
  463. <para>
  464. To extend the Front Controller, at the very minimum you will need
  465. to override the <code>getInstance()</code> method:
  466. </para>
  467. <programlisting language="php"><![CDATA[
  468. class My_Controller_Front extends Zend_Controller_Front
  469. {
  470. public static function getInstance()
  471. {
  472. if (null === self::$_instance) {
  473. self::$_instance = new self();
  474. }
  475. return self::$_instance;
  476. }
  477. }
  478. ]]></programlisting>
  479. <para>
  480. Overriding the <code>getInstance()</code> method ensures that
  481. subsequent calls to
  482. <classname>Zend_Controller_Front::getInstance()</classname> will return an
  483. instance of your new subclass instead of a
  484. <classname>Zend_Controller_Front</classname> instance -- this is particularly
  485. useful for some of the alternate routers and view helpers.
  486. </para>
  487. <para>
  488. Typically, you will not need to subclass the front controller unless
  489. you need to add new functionality (for instance, a plugin
  490. autoloader, or a way to specify action helper paths). Some points
  491. where you may want to alter behaviour may include modifying how
  492. controller directories are stored, or what default router or
  493. dispatcher are used.
  494. </para>
  495. </sect2>
  496. </sect1>
  497. <!--
  498. vim:se ts=4 sw=4 et:
  499. -->