Zend_Controller-FrontController.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <sect1 id="zend.controller.front">
  2. <title>Az elülső vezérlő</title>
  3. <sect2 id="zend.controller.front.overview">
  4. <title>Áttekintés</title>
  5. <para>
  6. A
  7. <code>Zend_Controller_Front</code>
  8. egy
  9. <ulink url="http://hu.wikipedia.org/wiki/Modell-nézet-vezérlő">Modell-Nézet-Vezérlő</ulink>
  10. (MNV) alkalmazásokban használatos
  11. <ulink url="http://www.martinfowler.com/eaaCatalog/frontController.html">Elülső Vezérlő</ulink>
  12. mintát valósít meg. Feladata, hogy előkészítse a kéréskörnyezetet, irányítsa a bejövő kérést, és kézbesítsen minden felfedezett műveletnek; összegyűjt minden választ és visszatér velük, amikor a folyamat befejeződik.
  13. </para>
  14. <para>
  15. A
  16. <code>Zend_Controller_Front</code>
  17. megvalósítja az
  18. <ulink url="http://en.wikipedia.org/wiki/Singleton_pattern">Egyke</ulink>
  19. mintát is, ami annyit tesz, hogy mindig csak egy példány lehet belőle elérhető. Ez lehetővé teszi, hogy egy a kézbesítési folyamat más objektumai által igénybevehető nyílvántartásként is működjön.
  20. </para>
  21. <para>
  22. A
  23. <code>Zend_Controller_Front</code>
  24. bejegyez magával egy
  25. <link linkend="zend.controller.plugins">bővítménykezelőt</link>,
  26. lehetővé téve, hogy bővítmények figyelhessék a különböző eseményeket, amiket kivált. Ez a legtöbb esetben biztosítja a fejlesztőnek a lehetőséget, hogy a célnak megfelelően alakítsa a kézbesítési folyamatot, anélkül, hogy funkcionalitás hozzáadásához ki kelljen terjesztenie az elülső vezérlőt.
  27. </para>
  28. <para>
  29. Munkája elvégzése érdekében az elülső vezérlőnek legkevesebb egy
  30. <link linkend="zend.controller.action">műveletvezérlőket</link>
  31. tartalmazó könyvtár elérési útjára van szüksége. Különféle más tagfüggvények is meghívhatók az elülső vezérlő és segéd osztályai környezetének további testreszabásához.
  32. </para>
  33. <note>
  34. <title>Alapértelmezett viselkedés</title>
  35. <para>
  36. Alapból az elülső vezérlő betölti az
  37. <link linkend="zend.controller.plugins.standard.errorhandler">ErrorHandler</link>
  38. bővítményt, akárcsak a
  39. <link linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>
  40. műveletsegéd bővítményt. Ezek a hibakezelés és a nézet megjelenítés egyszerűsítésére vannak, ebben a sorrendben.
  41. </para>
  42. <para>
  43. Az
  44. <code>ErrorHandler</code>
  45. kikapcsolásához hajtsd végre a következőt, bármikor a
  46. <code>dispatch()</code>
  47. meghívása előtt:
  48. </para>
  49. <programlisting role="php"><![CDATA[<?php
  50. //Az ErrorHandler bővítmény kikapcsolása:
  51. $front->setParam('noErrorHandler', true);]]>
  52. </programlisting>
  53. <para>
  54. A
  55. <code>ViewRenderer</code>
  56. kikapcsolásához tedd a következőt a
  57. <code>dispatch()</code>
  58. hívása előtt:
  59. </para>
  60. <programlisting role="php"><![CDATA[<?php
  61. //A ViewRenderer segéd kikapcsolása:
  62. $front->setParam('noViewRenderer', true);]]>
  63. </programlisting>
  64. </note>
  65. </sect2>
  66. <sect2 id="zend.controller.front.methods.primary">
  67. <title>Primary Methods</title>
  68. <para>
  69. The front controller has several accessors for setting up its
  70. environment. However, there are three primary methods key to the
  71. front controller's functionality:
  72. </para>
  73. <sect3 id="zend.controller.front.methods.primary.getinstance">
  74. <title>getInstance()</title>
  75. <para>
  76. <code>getInstance()</code> is used to retrieve a front
  77. controller instance. As the front controller implements a
  78. Singleton pattern, this is also the only means possible for
  79. instantiating a front controller object.
  80. </para>
  81. <programlisting role="php"><![CDATA[<?php
  82. $front = Zend_Controller_Front::getInstance();]]>
  83. </programlisting>
  84. </sect3>
  85. <sect3 id="zend.controller.front.methods.primary.setcontrollerdirectory">
  86. <title>setControllerDirectory() and addControllerDirectory</title>
  87. <para>
  88. <code>setControllerDirectory()</code> is used to tell <link
  89. linkend="zend.controller.dispatcher">the dispatcher</link>
  90. where to look for <link
  91. linkend="zend.controller.action">action controller</link>
  92. class files. It accepts either a single path or an associative
  93. array of module/path pairs.
  94. </para>
  95. <para>
  96. As some examples:
  97. </para>
  98. <programlisting role="php"><![CDATA[
  99. // Set the default controller directory:
  100. $front->setControllerDirectory('../application/controllers');
  101. // Set several module directories at once:
  102. $front->setControllerDirectory(array(
  103. 'default' => '../application/controllers',
  104. 'blog' => '../modules/blog/controllers',
  105. 'news' => '../modules/news/controllers',
  106. ));
  107. // Add a 'foo' module directory:
  108. $front->addControllerDirectory('../modules/foo/controllers', 'foo');]]>
  109. </programlisting>
  110. <note>
  111. <para>
  112. If you use <code>addControllerDirectory()</code> without a
  113. module name, it will set the directory for the
  114. <code>default</code> module -- overwriting it if it already
  115. exists.
  116. </para>
  117. </note>
  118. <para>
  119. You can get the current settings for the controller directory
  120. using <code>getControllerDirectory()</code>; this will return an
  121. array of module/directory pairs.
  122. </para>
  123. </sect3>
  124. <sect3 id="zend.controller.front.methods.primary.dispatch">
  125. <title>dispatch()</title>
  126. <para>
  127. <code>dispatch(Zend_Controller_Request_Abstract $request = null,
  128. Zend_Controller_Response_Abstract $response = null)</code>
  129. does the heavy work of the front controller. It may optionally
  130. take a <link linkend="zend.controller.request">request
  131. object</link> and/or a <link
  132. linkend="zend.controller.response">response object</link>,
  133. allowing the developer to pass in custom objects for each.
  134. </para>
  135. <para>
  136. If no request or response object are passed in,
  137. <code>dispatch()</code> will check for previously registered
  138. objects and use those or instantiate default versions to use in
  139. its process (in both cases, the HTTP flavor will be used as the
  140. default).
  141. </para>
  142. <para>
  143. Similarly, <code>dispatch()</code> checks for registered <link
  144. linkend="zend.controller.router">router</link> and <link
  145. linkend="zend.controller.dispatcher">dispatcher</link>
  146. objects, instantiating the default versions of each if none is
  147. found.
  148. </para>
  149. <para>
  150. The dispatch process has three distinct events:
  151. </para>
  152. <itemizedlist>
  153. <listitem><para>Routing</para></listitem>
  154. <listitem><para>Dispatching</para></listitem>
  155. <listitem><para>Response</para></listitem>
  156. </itemizedlist>
  157. <para>
  158. Routing takes place exactly once, using the values in the
  159. request object when <code>dispatch()</code> is called.
  160. Dispatching takes place in a loop; a request may either indicate
  161. multiple actions to dispatch, or the controller or a plugin may
  162. reset the request object to force additional actions to
  163. dispatch. When all is done, the front controller returns a
  164. response.
  165. </para>
  166. </sect3>
  167. <sect3 id="zend.controller.front.methods.primary.run">
  168. <title>run()</title>
  169. <para>
  170. <code>Zend_Controller_Front::run($path)</code> is a static
  171. method taking simply a path to a directory containing
  172. controllers. It fetches a front controller instance (via
  173. <link
  174. linkend="zend.controller.front.methods.primary.getinstance">getInstance()</link>),
  175. registers the path provided via <link
  176. linkend="zend.controller.front.methods.primary.setcontrollerdirectory">setControllerDirectory()</link>,
  177. and finally <link
  178. linkend="zend.controller.front.methods.primary.dispatch">dispatches</link>.
  179. </para>
  180. <para>
  181. Basically, <code>run()</code> is a convenience method that can
  182. be used for site setups that do not require customization of the
  183. front controller environment.
  184. </para>
  185. <programlisting role="php"><![CDATA[<?php
  186. // Instantiate front controller, set controller directory, and dispatch in one
  187. // easy step:
  188. Zend_Controller_Front::run('../application/controllers');]]>
  189. </programlisting>
  190. </sect3>
  191. </sect2>
  192. <sect2 id="zend.controller.front.methods.environment">
  193. <title>Environmental Accessor Methods</title>
  194. <para>
  195. In addition to the methods listed above, there are a number of
  196. accessor methods that can be used to affect the front controller
  197. environment -- and thus the environment of the classes to which the
  198. front controller delegates.
  199. </para>
  200. <itemizedlist>
  201. <listitem>
  202. <para>
  203. <code>resetInstance()</code> can be used to clear all
  204. current settings. Its primary purpose is for testing, but it
  205. can also be used for instances where you wish to chain
  206. together multiple front controllers.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. <code>(set|get)DefaultControllerName()</code> let you
  212. specify a different name to use for the default controller
  213. ('index' is used otherwise) and retrieve the current value.
  214. They proxy to <link
  215. linkend="zend.controller.dispatcher">the
  216. dispatcher</link>.
  217. </para>
  218. </listitem>
  219. <listitem>
  220. <para>
  221. <code>(set|get)DefaultActionName()</code> let you specify a
  222. different name to use for the default action ('index' is
  223. used otherwise) and retrieve the current value. They proxy
  224. to <link linkend="zend.controller.dispatcher">the
  225. dispatcher</link>.
  226. </para>
  227. </listitem>
  228. <listitem>
  229. <para>
  230. <code>(set|get)Request()</code> let you specify <link
  231. linkend="zend.controller.request">the request</link>
  232. class or object to use during the dispatch process and to
  233. retrieve the current object. When setting the request
  234. object, you may pass in a request class name, in which case
  235. the method will load the class file and instantiate it.
  236. </para>
  237. </listitem>
  238. <listitem>
  239. <para>
  240. <code>(set|get)Router()</code> let you specify <link
  241. linkend="zend.controller.router">the router</link>
  242. class or object to use during the dispatch process and to
  243. retrieve the current object. When setting the router
  244. object, you may pass in a router class name, in which case
  245. the method will load the class file and instantiate it.
  246. </para>
  247. <para>
  248. When retrieving the router object, it first checks to see if
  249. one is present, and if not, instantiates the default router
  250. (rewrite router).
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <code>(set|get)BaseUrl()</code> let you specify <link
  256. linkend="zend.controller.request.http.baseurl">the base
  257. URL</link> to strip when routing requests and to
  258. retrieve the current value. The value is provided to the
  259. request object just prior to routing.
  260. </para>
  261. </listitem>
  262. <listitem>
  263. <para>
  264. <code>(set|get)Dispatcher()</code> let you specify <link
  265. linkend="zend.controller.dispatcher">the
  266. dispatcher</link> class or object to use during the
  267. dispatch process and retrieve the current object. When
  268. setting the dispatcher object, you may pass in a dispatcher
  269. class name, in which case the method will load the class
  270. file and instantiate it.
  271. </para>
  272. <para>
  273. When retrieving the dispatcher object, it first checks to see if
  274. one is present, and if not, instantiates the default
  275. dispatcher.
  276. </para>
  277. </listitem>
  278. <listitem>
  279. <para>
  280. <code>(set|get)Response()</code> let you specify <link
  281. linkend="zend.controller.response">the response</link>
  282. class or object to use during the dispatch process and to
  283. retrieve the current object. When setting the response
  284. object, you may pass in a response class name, in which case
  285. the method will load the class file and instantiate it.
  286. </para>
  287. </listitem>
  288. <listitem>
  289. <para>
  290. <code>registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)</code>
  291. allows you to register a <link
  292. linkend="zend.controller.plugins">plugin objects</link>.
  293. By setting the optional <code>$stackIndex</code>, you can
  294. control the order in which plugins will execute.
  295. </para>
  296. </listitem>
  297. <listitem>
  298. <para>
  299. <code>unregisterPlugin($plugin)</code> let you
  300. unregister <link
  301. linkend="zend.controller.plugins">plugin objects</link>.
  302. <code>$plugin</code> may be either a plugin object or a
  303. string denoting the class of plugin to unregister.
  304. </para>
  305. </listitem>
  306. <listitem>
  307. <para>
  308. <code>throwExceptions($flag)</code> is used to turn on/off
  309. the ability to throw exceptions during the dispatch process.
  310. By default, exceptions are caught and placed in the <link
  311. linkend="zend.controller.response">response
  312. object</link>; turning on <code>throwExceptions()</code>
  313. will override this behaviour.
  314. </para>
  315. <para>
  316. For more information, read <xref
  317. linkend="zend.controller.exceptions" />.
  318. </para>
  319. </listitem>
  320. <listitem>
  321. <para>
  322. <code>returnResponse($flag)</code> is used to tell the front
  323. controller whether to return the response
  324. (<code>true</code>) from <code>dispatch()</code>, or if the
  325. response should be automatically emitted
  326. (<code>false</code>). By default, the response is
  327. automatically emitted (by calling
  328. <code>Zend_Controller_Response_Abstract::sendResponse()</code>);
  329. turning on <code>returnResponse()</code> will override this
  330. behaviour.
  331. </para>
  332. <para>
  333. Reasons to return the response include a desire to check for
  334. exceptions prior to emitting the response, needing to log
  335. various aspects of the response (such as headers), etc.
  336. </para>
  337. </listitem>
  338. </itemizedlist>
  339. </sect2>
  340. <sect2 id="zend.controller.front.methods.params">
  341. <title>Front Controller Parameters</title>
  342. <para>
  343. In the introduction, we indicated that the front controller also
  344. acts as a registry for the various controller components. It does so
  345. through a family of "param" methods. These methods allow you to
  346. register arbitrary data -- objects and variables -- with the front
  347. controller to be retrieved at any time in the dispatch chain. These
  348. values are passed on to the router, dispatcher, and action
  349. controllers. The methods include:
  350. </para>
  351. <itemizedlist>
  352. <listitem>
  353. <para>
  354. <code>setParam($name, $value)</code> allows you to set a
  355. single parameter of <code>$name</code> with value
  356. <code>$value</code>.
  357. </para>
  358. </listitem>
  359. <listitem>
  360. <para>
  361. <code>setParams(array $params)</code> allows you to set
  362. multiple parameters at once using an associative array.
  363. </para>
  364. </listitem>
  365. <listitem>
  366. <para>
  367. <code>getParam($name)</code> allows you to retrieve a single
  368. parameter at a time, using <code>$name</code> as the
  369. identifier.
  370. </para>
  371. </listitem>
  372. <listitem>
  373. <para>
  374. <code>getParams()</code> allows you to retrieve the entire
  375. list of parameters at once.
  376. </para>
  377. </listitem>
  378. <listitem>
  379. <para>
  380. <code>clearParams()</code> allows you to clear a single
  381. parameter (by passing a string identifier), multiple named
  382. parameters (by passing an array of string identifiers), or the
  383. entire parameter stack (by passing nothing).
  384. </para>
  385. </listitem>
  386. </itemizedlist>
  387. <para>
  388. There are several pre-defined parameters that may be set that have
  389. specific uses in the dispatch chain:
  390. </para>
  391. <itemizedlist>
  392. <listitem>
  393. <para>
  394. <code>useDefaultControllerAlways</code> is used to hint to
  395. <link linkend="zend.controller.dispatcher">the
  396. dispatcher</link> to use the default controller in the
  397. default module for any request that is not dispatchable
  398. (i.e., the module, controller, and/or action do not exist).
  399. By default, this is off.
  400. </para>
  401. <para>
  402. See <xref linkend="zend.controller.exceptions.internal" />
  403. for more detailed information on using this setting.
  404. </para>
  405. </listitem>
  406. <listitem>
  407. <para>
  408. <code>disableOutputBuffering</code> is used to hint to <link
  409. linkend="zend.controller.dispatcher">the
  410. dispatcher</link> that it should not use output
  411. buffering to capture output generated by action controllers.
  412. By default, the dispatcher captures any output and appends
  413. it to the response object body content.
  414. </para>
  415. </listitem>
  416. <listitem>
  417. <para>
  418. <code>noViewRenderer</code> is used to disable the <link
  419. linkend="zend.controller.actionhelpers.viewrenderer">ViewRenderer</link>.
  420. Set this parameter to true to disable it.
  421. </para>
  422. </listitem>
  423. <listitem>
  424. <para>
  425. <code>noErrorHandler</code> is used to disable the <link
  426. linkend="zend.controller.plugins.standard.errorhandler">Error
  427. Handler plugin</link>. Set this parameter to true to
  428. disable it.
  429. </para>
  430. </listitem>
  431. </itemizedlist>
  432. </sect2>
  433. <sect2 id="zend.controller.front.subclassing">
  434. <title>Subclassing the Front Controller</title>
  435. <para>
  436. To subclass the Front Controller, at the very minimum you will need
  437. to override the <code>getInstance()</code> method:
  438. </para>
  439. <programlisting role="php"><![CDATA[
  440. class My_Controller_Front extends Zend_Controller_Front
  441. {
  442. public static function getInstance()
  443. {
  444. if (null === self::$_instance) {
  445. self::$_instance = new self();
  446. }
  447. return self::$_instance;
  448. }
  449. }
  450. ]]>
  451. </programlisting>
  452. <para>
  453. Overriding the <code>getInstance()</code> method ensures that
  454. subsequent calls to
  455. <code>Zend_Controller_Front::getInstance()</code> will return an
  456. instance of your new subclass instead of a
  457. <code>Zend_Controller_Front</code> instance -- this is particularly
  458. useful for some of the alternate routers and view helpers.
  459. </para>
  460. <para>
  461. Typically, you will not need to subclass the front controller unless
  462. you need to add new functionality (for instance, a plugin
  463. autoloader, or a way to specify action helper paths). Some points
  464. where you may want to alter behaviour may include modifying how
  465. controller directories are stored, or what default router or
  466. dispatcher are used.
  467. </para>
  468. </sect2>
  469. </sect1>
  470. <!--
  471. vim:se ts=4 sw=4 et:
  472. -->