2
0

Zend_Controller-Plugins.xml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.plugins" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <title>Plugins</title>
  5. <sect2 id="zend.controller.plugins.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The controller architecture includes a plugin system that allows
  9. user code to be called when certain events occur in the controller
  10. process lifetime. The front controller uses a plugin broker as a
  11. registry for user plugins, and the plugin broker ensures that event
  12. methods are called on each plugin registered with the front
  13. controller.
  14. </para>
  15. <para>
  16. The event methods are defined in the abstract class
  17. <classname>Zend_Controller_Plugin_Abstract</classname>, from which user plugin
  18. classes inherit:
  19. </para>
  20. <itemizedlist>
  21. <listitem>
  22. <para>
  23. <methodname>routeStartup()</methodname> is called before
  24. <classname>Zend_Controller_Front</classname> calls on <link
  25. linkend="zend.controller.router">the router</link>
  26. to evaluate the request against the registered routes.
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. <methodname>routeShutdown()</methodname> is called after <link
  32. linkend="zend.controller.router">the router</link>
  33. finishes routing the request.
  34. </para>
  35. </listitem>
  36. <listitem>
  37. <para>
  38. <methodname>dispatchLoopStartup()</methodname> is called before
  39. <classname>Zend_Controller_Front</classname> enters its dispatch loop.
  40. </para>
  41. </listitem>
  42. <listitem>
  43. <para>
  44. <methodname>preDispatch()</methodname> is called before an action is
  45. dispatched by <link linkend="zend.controller.dispatcher">the
  46. dispatcher</link>. This callback allows for proxy or
  47. filter behavior. By altering the request and resetting its
  48. dispatched flag (via
  49. <methodname>Zend_Controller_Request_Abstract::setDispatched(false)</methodname>),
  50. the current action may be skipped and/or replaced.
  51. </para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. <methodname>postDispatch()</methodname> is called after an action is
  56. dispatched by <link linkend="zend.controller.dispatcher">the
  57. dispatcher</link>. This callback allows for proxy or
  58. filter behavior. By altering the request and resetting its
  59. dispatched flag (via
  60. <methodname>Zend_Controller_Request_Abstract::setDispatched(false)</methodname>),
  61. a new action may be specified for dispatching.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. <methodname>dispatchLoopShutdown()</methodname> is called after
  67. <classname>Zend_Controller_Front</classname> exits its dispatch loop.
  68. </para>
  69. </listitem>
  70. </itemizedlist>
  71. </sect2>
  72. <sect2 id="zend.controller.plugins.writing">
  73. <title>Writing Plugins</title>
  74. <para>
  75. In order to write a plugin class, simply include and extend the
  76. abstract class <classname>Zend_Controller_Plugin_Abstract</classname>:
  77. </para>
  78. <programlisting language="php"><![CDATA[
  79. class MyPlugin extends Zend_Controller_Plugin_Abstract
  80. {
  81. // ...
  82. }
  83. ]]></programlisting>
  84. <para>
  85. None of the methods of <classname>Zend_Controller_Plugin_Abstract</classname>
  86. are abstract, and this means that plugin classes are not forced to
  87. implement any of the available event methods listed above. Plugin
  88. writers may implement only those methods required by their
  89. particular needs.
  90. </para>
  91. <para>
  92. <classname>Zend_Controller_Plugin_Abstract</classname> also makes the request
  93. and response objects available to controller plugins via the
  94. <methodname>getRequest()</methodname> and <methodname>getResponse()</methodname>
  95. methods, respectively.
  96. </para>
  97. </sect2>
  98. <sect2 id="zend.controller.plugins.using">
  99. <title>Using Plugins</title>
  100. <para>
  101. Plugin classes are registered with
  102. <methodname>Zend_Controller_Front::registerPlugin()</methodname>, and may be
  103. registered at any time. The following snippet illustrates how a
  104. plugin may be used in the controller chain:
  105. </para>
  106. <programlisting language="php"><![CDATA[
  107. class MyPlugin extends Zend_Controller_Plugin_Abstract
  108. {
  109. public function routeStartup(Zend_Controller_Request_Abstract $request)
  110. {
  111. $this->getResponse()
  112. ->appendBody("<p>routeStartup() called</p>\n");
  113. }
  114. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  115. {
  116. $this->getResponse()
  117. ->appendBody("<p>routeShutdown() called</p>\n");
  118. }
  119. public function dispatchLoopStartup(
  120. Zend_Controller_Request_Abstract $request)
  121. {
  122. $this->getResponse()
  123. ->appendBody("<p>dispatchLoopStartup() called</p>\n");
  124. }
  125. public function preDispatch(Zend_Controller_Request_Abstract $request)
  126. {
  127. $this->getResponse()
  128. ->appendBody("<p>preDispatch() called</p>\n");
  129. }
  130. public function postDispatch(Zend_Controller_Request_Abstract $request)
  131. {
  132. $this->getResponse()
  133. ->appendBody("<p>postDispatch() called</p>\n");
  134. }
  135. public function dispatchLoopShutdown()
  136. {
  137. $this->getResponse()
  138. ->appendBody("<p>dispatchLoopShutdown() called</p>\n");
  139. }
  140. }
  141. $front = Zend_Controller_Front::getInstance();
  142. $front->setControllerDirectory('/path/to/controllers')
  143. ->setRouter(new Zend_Controller_Router_Rewrite())
  144. ->registerPlugin(new MyPlugin());
  145. $front->dispatch();
  146. ]]></programlisting>
  147. <para>
  148. Assuming that no actions called emit any output, and only one action
  149. is called, the functionality of the above plugin would still create
  150. the following output:
  151. </para>
  152. <programlisting language="php"><![CDATA[
  153. <p>routeStartup() called</p>
  154. <p>routeShutdown() called</p>
  155. <p>dispatchLoopStartup() called</p>
  156. <p>preDispatch() called</p>
  157. <p>postDispatch() called</p>
  158. <p>dispatchLoopShutdown() called</p>
  159. ]]></programlisting>
  160. <note>
  161. <para>
  162. Plugins may be registered at any time during the front
  163. controller execution. However, if an event has passed for which
  164. the plugin has a registered event method, that method will not
  165. be triggered.
  166. </para>
  167. </note>
  168. </sect2>
  169. <sect2 id="zend.controller.plugins.manipulating">
  170. <title>Retrieving and Manipulating Plugins</title>
  171. <para>
  172. On occasion, you may need to unregister or retrieve a plugin. The
  173. following methods of the front controller allow you to do so:
  174. </para>
  175. <itemizedlist>
  176. <listitem>
  177. <para>
  178. <methodname>getPlugin($class)</methodname> allows you to retrieve a
  179. plugin by class name. If no plugins match, it returns
  180. <constant>FALSE</constant>. If more than one plugin of that class
  181. is registered, it returns an array.
  182. </para>
  183. </listitem>
  184. <listitem>
  185. <para>
  186. <methodname>getPlugins()</methodname> retrieves the entire plugin stack.
  187. </para>
  188. </listitem>
  189. <listitem>
  190. <para>
  191. <methodname>unregisterPlugin($plugin)</methodname> allows you to remove
  192. a plugin from the stack. You may pass a plugin object, or
  193. the class name of the plugin you wish to unregister. If you
  194. pass the class name, any plugins of that class will be
  195. removed.
  196. </para>
  197. </listitem>
  198. </itemizedlist>
  199. </sect2>
  200. <sect2 id="zend.controller.plugins.standard">
  201. <title>Plugins Included in the Standard Distribution</title>
  202. <para>
  203. Zend Framework includes a plugin for error handling in its standard
  204. distribution.
  205. </para>
  206. <xi:include href="Zend_Controller-Plugins-ActionStack.xml" />
  207. <xi:include href="Zend_Controller-Plugins-ErrorHandler.xml" />
  208. <xi:include href="Zend_Controller-Plugins-PutHandler.xml" />
  209. </sect2>
  210. </sect1>
  211. <!--
  212. vim:se ts=4 sw=4 et:
  213. -->