Zend_Controller-ActionHelpers.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.actionhelpers" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <title>Action Helpers</title>
  5. <sect2 id="zend.controller.actionhelper.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. Action Helpers allow developers to inject runtime and/or on-demand
  9. functionality into any Action Controllers that extend
  10. <classname>Zend_Controller_Action</classname>. Action Helpers aim to minimize the
  11. necessity to extend the abstract Action Controller in order to
  12. inject common Action Controller functionality.
  13. </para>
  14. <para>
  15. There are a number of ways to use Action Helpers. Action Helpers
  16. employ the use of a brokerage system, similar to the types of
  17. brokerage you see in <link
  18. linkend="zend.view.helpers">Zend_View_Helper</link>, and that
  19. of <link
  20. linkend="zend.controller.plugins">Zend_Controller_Plugin</link>.
  21. Action Helpers (like <classname>Zend_View_Helper</classname>) may be
  22. loaded and called on demand, or they may be instantiated at
  23. request time (bootstrap) or action controller creation time
  24. (init()). To understand this more fully, please see the usage
  25. section below.
  26. </para>
  27. </sect2>
  28. <sect2 id="zend.controller.actionhelper.initialization">
  29. <title>Helper Initialization</title>
  30. <para>
  31. A helper can be initialized in several different ways, based on
  32. your needs as well as the functionality of that helper.
  33. </para>
  34. <para>
  35. The helper broker is stored as the <varname>$_helper</varname> member of
  36. <classname>Zend_Controller_Action</classname>; use the broker to retrieve or
  37. call on helpers. Some methods for doing so include:
  38. </para>
  39. <itemizedlist>
  40. <listitem>
  41. <para>
  42. Explicitly using <code>getHelper()</code>. Simply pass it a
  43. name, and a helper object is returned:
  44. </para>
  45. <programlisting language="php"><![CDATA[
  46. $flashMessenger = $this->_helper->getHelper('FlashMessenger');
  47. $flashMessenger->addMessage('We did something in the last request');
  48. ]]></programlisting>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. Use the helper broker's <code>__get()</code> functionality
  53. and retrieve the helper as if it were a member property of
  54. the broker:
  55. </para>
  56. <programlisting language="php"><![CDATA[
  57. $flashMessenger = $this->_helper->FlashMessenger;
  58. $flashMessenger->addMessage('We did something in the last request');
  59. ]]></programlisting>
  60. </listitem>
  61. <listitem>
  62. <para>
  63. Finally, most action helpers implement the method
  64. <code>direct()</code> which will call a specific, default
  65. method in the helper. In the example of the
  66. <code>FlashMessenger</code>, it calls
  67. <code>addMessage()</code>:
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. $this->_helper->FlashMessenger('We did something in the last request');
  71. ]]></programlisting>
  72. </listitem>
  73. </itemizedlist>
  74. <note>
  75. <para>All of the above examples are functionally equivalent.</para>
  76. </note>
  77. <para>
  78. You may also instantiate helpers explicitly. You may wish to do this
  79. if using the helper outside of an action controller, or if you wish
  80. to pass a helper to the helper broker for use by any action.
  81. Instantiation is as per any other PHP class.
  82. </para>
  83. </sect2>
  84. <sect2 id="zend.controller.actionhelper.broker">
  85. <title>The Helper Broker</title>
  86. <para>
  87. <classname>Zend_Controller_Action_HelperBroker</classname> handles the details
  88. of registering helper objects and helper paths, as well as
  89. retrieving helpers on-demand.
  90. </para>
  91. <para>
  92. To register a helper with the broker, use <code>addHelper</code>:
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. Zend_Controller_Action_HelperBroker::addHelper($helper);
  96. ]]></programlisting>
  97. <para>
  98. Of course, instantiating and passing helpers to the broker is a bit
  99. time and resource intensive, so two methods exists to automate
  100. things slightly: <code>addPrefix()</code> and
  101. <code>addPath()</code>.
  102. </para>
  103. <itemizedlist>
  104. <listitem>
  105. <para>
  106. <code>addPrefix()</code> takes a class prefix and uses it
  107. to determine a path where helper classes have been defined.
  108. It assumes the prefix follows Zend Framework class naming
  109. conventions.
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. // Add helpers prefixed with My_Action_Helpers in My/Action/Helpers/
  113. Zend_Controller_Action_HelperBroker::addPrefix('My_Action_Helpers');
  114. ]]></programlisting>
  115. </listitem>
  116. <listitem>
  117. <para>
  118. <code>addPath()</code> takes a directory as its first
  119. argument and a class prefix as the second argument
  120. (defaulting to 'Zend_Controller_Action_Helper'). This allows
  121. you to map your own class prefixes to specific directories.
  122. </para>
  123. <programlisting language="php"><![CDATA[
  124. // Add helpers prefixed with Helper in Plugins/Helpers/
  125. Zend_Controller_Action_HelperBroker::addPath('./Plugins/Helpers',
  126. 'Helper');
  127. ]]></programlisting>
  128. </listitem>
  129. </itemizedlist>
  130. <para>
  131. Since these methods are static, they may be called at any point in
  132. the controller chain in order to dynamically add helpers as needed.
  133. </para>
  134. <para>
  135. Internally, the helper broker uses <link
  136. linkend="zend.loader.pluginloader">a PluginLoader
  137. instance</link> to maintain paths. You can retrieve the
  138. PluginLoader using the static method <code>getPluginLoader()</code>,
  139. or, alternately, inject a custom PluginLoader instance using
  140. <code>setPluginLoader()</code>.
  141. </para>
  142. <para>
  143. To determine if a helper exists in the helper broker, use
  144. <code>hasHelper($name)</code>, where <varname>$name</varname> is the short
  145. name of the helper (minus the prefix):
  146. </para>
  147. <programlisting language="php"><![CDATA[
  148. // Check if 'redirector' helper is registered with the broker:
  149. if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
  150. echo 'Redirector helper registered';
  151. }
  152. ]]></programlisting>
  153. <para>
  154. There are also two static methods for retrieving helpers from the helper
  155. broker: <code>getExistingHelper()</code> and
  156. <code>getStaticHelper()</code>. <code>getExistingHelper()</code>
  157. will retrieve a helper only if it has previously been invoked by or
  158. explicitly registered with the helper broker; it will throw an
  159. exception if not. <code>getStaticHelper()</code> does the same as
  160. <code>getExistingHelper()</code>, but will attempt to instantiate
  161. the helper if has not yet been registered with the helper stack.
  162. <code>getStaticHelper()</code> is a good choice for retrieving
  163. helpers which you wish to configure.
  164. </para>
  165. <para>
  166. Both methods take a single argument, <varname>$name</varname>, which is
  167. the short name of the helper (minus the prefix).
  168. </para>
  169. <programlisting language="php"><![CDATA[
  170. // Check if 'redirector' helper is registered with the broker, and fetch:
  171. if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
  172. $redirector =
  173. Zend_Controller_Action_HelperBroker::getExistingHelper('redirector');
  174. }
  175. // Or, simply retrieve it, not worrying about whether or not it was
  176. // previously registered:
  177. $redirector =
  178. Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
  179. }
  180. ]]></programlisting>
  181. <para>
  182. Finally, to delete a registered helper from the broker, use
  183. <code>removeHelper($name)</code>, where <varname>$name</varname> is the
  184. short name of the helper (minus the prefix):
  185. </para>
  186. <programlisting language="php"><![CDATA[
  187. // Conditionally remove the 'redirector' helper from the broker:
  188. if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
  189. Zend_Controller_Action_HelperBroker::removeHelper('redirector')
  190. }
  191. ]]></programlisting>
  192. </sect2>
  193. <sect2 id="zend.controller.actionhelper.stockhelpers">
  194. <title>Built-in Action Helpers</title>
  195. <para>
  196. Zend Framework includes several action helpers by default:
  197. <code>AutoComplete</code> for automating responses for AJAX
  198. autocompletion; <code>ContextSwitch</code> and
  199. <code>AjaxContext</code> for serving alternate response formats for
  200. your actions; a <code>FlashMessenger</code> for handling session
  201. flash messages; <code>Json</code> for encoding and sending JSON
  202. responses; a <code>Redirector</code>, to provide different
  203. implementations for redirecting to internal and external pages from
  204. your application; and a <code>ViewRenderer</code> to automate the
  205. process of setting up the view object in your controllers and
  206. rendering views.
  207. </para>
  208. <xi:include href="Zend_Controller-ActionHelpers-ActionStack.xml" />
  209. <xi:include href="Zend_Controller-ActionHelpers-AutoComplete.xml" />
  210. <xi:include href="Zend_Controller-ActionHelpers-ContextSwitch.xml" />
  211. <xi:include href="Zend_Controller-ActionHelpers-FlashMessenger.xml" />
  212. <xi:include href="Zend_Controller-ActionHelpers-Json.xml" />
  213. <xi:include href="Zend_Controller-ActionHelpers-Redirector.xml" />
  214. <xi:include href="Zend_Controller-ActionHelpers-ViewRenderer.xml" />
  215. </sect2>
  216. <sect2 id="zend.controller.actionhelper.writingyourown">
  217. <title>Writing Your Own Helpers</title>
  218. <para>
  219. Action helpers extend
  220. <classname>Zend_Controller_Action_Helper_Abstract</classname>, an abstract
  221. class that provides the basic interface and functionality required
  222. by the helper broker. These include the following methods:
  223. </para>
  224. <itemizedlist>
  225. <listitem>
  226. <para>
  227. <code>setActionController()</code> is used to set the current
  228. action controller.
  229. </para>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. <code>init()</code>, triggered by the helper broker at
  234. instantiation, can be used to trigger initialization in the
  235. helper; this can be useful for resetting state when multiple
  236. controllers use the same helper in chained actions.
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>
  241. <code>preDispatch()</code>, is triggered prior to a
  242. dispatched action.
  243. </para>
  244. </listitem>
  245. <listitem>
  246. <para>
  247. <code>postDispatch()</code> is triggered when a dispatched
  248. action is done -- even if a <code>preDispatch()</code>
  249. plugin has skipped the action. Mainly useful for cleanup.
  250. </para>
  251. </listitem>
  252. <listitem>
  253. <para>
  254. <code>getRequest()</code> retrieves the current request
  255. object.
  256. </para>
  257. </listitem>
  258. <listitem>
  259. <para>
  260. <code>getResponse()</code> retrieves the current response
  261. object.
  262. </para>
  263. </listitem>
  264. <listitem>
  265. <para>
  266. <code>getName()</code> retrieves the helper name. It
  267. retrieves the portion of the class name following the last
  268. underscore character, or the full class name otherwise. As
  269. an example, if the class is named
  270. <classname>Zend_Controller_Action_Helper_Redirector</classname>, it
  271. will return <code>Redirector</code>; a class named
  272. <code>FooMessage</code> will simply return itself.
  273. </para>
  274. </listitem>
  275. </itemizedlist>
  276. <para>
  277. You may optionally include a <code>direct()</code> method in your
  278. helper class. If defined, it allows you to treat the helper as a
  279. method of the helper broker, in order to allow easy, one-off usage
  280. of the helper. As an example, the <link
  281. linkend="zend.controller.actionhelpers.redirector">redirector</link>
  282. defines <code>direct()</code> as an alias of <code>goto()</code>,
  283. allowing use of the helper like this:
  284. </para>
  285. <programlisting language="php"><![CDATA[
  286. // Redirect to /blog/view/item/id/42
  287. $this->_helper->redirector('item', 'view', 'blog', array('id' => 42));
  288. ]]></programlisting>
  289. <para>
  290. Internally, the helper broker's <code>__call()</code> method looks
  291. for a helper named <code>redirector</code>, then checks to see if
  292. that helper has a defined <code>direct</code> class, and calls it
  293. with the arguments provided.
  294. </para>
  295. <para>
  296. Once you have created your own helper class, you may provide access
  297. to it as described in the sections above.
  298. </para>
  299. </sect2>
  300. </sect1>
  301. <!--
  302. vim:se ts=4 sw=4 et:
  303. -->