Zend_Controller-ActionHelpers.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. (<methodname>init()</methodname>). 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 <methodname>getHelper()</methodname>. 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 <methodname>__get()</methodname> 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. <methodname>direct()</methodname> which will call a specific, default
  65. method in the helper. In the example of the
  66. <emphasis>FlashMessenger</emphasis>, it calls
  67. <methodname>addMessage()</methodname>:
  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 <acronym>PHP</acronym> 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 <methodname>addHelper()</methodname>:
  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: <methodname>addPrefix()</methodname> and
  101. <methodname>addPath()</methodname>.
  102. </para>
  103. <itemizedlist>
  104. <listitem>
  105. <para>
  106. <methodname>addPrefix()</methodname> 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. <methodname>addPath()</methodname> takes a directory as its first
  119. argument and a class prefix as the second argument
  120. (defaulting to '<classname>Zend_Controller_Action_Helper</classname>').
  121. This allows 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 <methodname>getPluginLoader()</methodname>,
  139. or, alternately, inject a custom PluginLoader instance using
  140. <methodname>setPluginLoader()</methodname>.
  141. </para>
  142. <para>
  143. To determine if a helper exists in the helper broker, use
  144. <methodname>hasHelper($name)</methodname>, 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: <methodname>getExistingHelper()</methodname> and
  156. <methodname>getStaticHelper()</methodname>.
  157. <methodname>getExistingHelper()</methodname>
  158. will retrieve a helper only if it has previously been invoked by or
  159. explicitly registered with the helper broker; it will throw an
  160. exception if not. <methodname>getStaticHelper()</methodname> does the same as
  161. <methodname>getExistingHelper()</methodname>, but will attempt to instantiate
  162. the helper if has not yet been registered with the helper stack.
  163. <methodname>getStaticHelper()</methodname> is a good choice for retrieving
  164. helpers which you wish to configure.
  165. </para>
  166. <para>
  167. Both methods take a single argument, <varname>$name</varname>, which is
  168. the short name of the helper (minus the prefix).
  169. </para>
  170. <programlisting language="php"><![CDATA[
  171. // Check if 'redirector' helper is registered with the broker, and fetch:
  172. if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
  173. $redirector =
  174. Zend_Controller_Action_HelperBroker::getExistingHelper('redirector');
  175. }
  176. // Or, simply retrieve it, not worrying about whether or not it was
  177. // previously registered:
  178. $redirector =
  179. Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
  180. }
  181. ]]></programlisting>
  182. <para>
  183. Finally, to delete a registered helper from the broker, use
  184. <methodname>removeHelper($name)</methodname>, where <varname>$name</varname> is the
  185. short name of the helper (minus the prefix):
  186. </para>
  187. <programlisting language="php"><![CDATA[
  188. // Conditionally remove the 'redirector' helper from the broker:
  189. if (Zend_Controller_Action_HelperBroker::hasHelper('redirector')) {
  190. Zend_Controller_Action_HelperBroker::removeHelper('redirector')
  191. }
  192. ]]></programlisting>
  193. </sect2>
  194. <sect2 id="zend.controller.actionhelper.stockhelpers">
  195. <title>Built-in Action Helpers</title>
  196. <para>
  197. Zend Framework includes several action helpers by default:
  198. <emphasis>AutoComplete</emphasis> for automating responses for <acronym>AJAX</acronym>
  199. autocompletion; <emphasis>ContextSwitch</emphasis> and
  200. <emphasis>AjaxContext</emphasis> for serving alternate response formats for
  201. your actions; a <emphasis>FlashMessenger</emphasis> for handling session flash
  202. messages; <emphasis>Json</emphasis> for encoding and sending <acronym>JSON</acronym>
  203. responses; a <emphasis>Redirector</emphasis>, to provide different
  204. implementations for redirecting to internal and external pages from
  205. your application; and a <emphasis>ViewRenderer</emphasis> to automate the
  206. process of setting up the view object in your controllers and
  207. rendering views.
  208. </para>
  209. <xi:include href="Zend_Controller-ActionHelpers-ActionStack.xml" />
  210. <xi:include href="Zend_Controller-ActionHelpers-AutoComplete.xml" />
  211. <xi:include href="Zend_Controller-ActionHelpers-ContextSwitch.xml" />
  212. <xi:include href="Zend_Controller-ActionHelpers-FlashMessenger.xml" />
  213. <xi:include href="Zend_Controller-ActionHelpers-Json.xml" />
  214. <xi:include href="Zend_Controller-ActionHelpers-Redirector.xml" />
  215. <xi:include href="Zend_Controller-ActionHelpers-ViewRenderer.xml" />
  216. </sect2>
  217. <sect2 id="zend.controller.actionhelper.writingyourown">
  218. <title>Writing Your Own Helpers</title>
  219. <para>
  220. Action helpers extend
  221. <classname>Zend_Controller_Action_Helper_Abstract</classname>, an abstract
  222. class that provides the basic interface and functionality required
  223. by the helper broker. These include the following methods:
  224. </para>
  225. <itemizedlist>
  226. <listitem>
  227. <para>
  228. <methodname>setActionController()</methodname> is used to set the current
  229. action controller.
  230. </para>
  231. </listitem>
  232. <listitem>
  233. <para>
  234. <methodname>init()</methodname>, triggered by the helper broker at
  235. instantiation, can be used to trigger initialization in the
  236. helper; this can be useful for resetting state when multiple
  237. controllers use the same helper in chained actions.
  238. </para>
  239. </listitem>
  240. <listitem>
  241. <para>
  242. <methodname>preDispatch()</methodname>, is triggered prior to a
  243. dispatched action.
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <methodname>postDispatch()</methodname> is triggered when a dispatched
  249. action is done -- even if a <methodname>preDispatch()</methodname>
  250. plugin has skipped the action. Mainly useful for cleanup.
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <methodname>getRequest()</methodname> retrieves the current request
  256. object.
  257. </para>
  258. </listitem>
  259. <listitem>
  260. <para>
  261. <methodname>getResponse()</methodname> retrieves the current response
  262. object.
  263. </para>
  264. </listitem>
  265. <listitem>
  266. <para>
  267. <methodname>getName()</methodname> retrieves the helper name. It
  268. retrieves the portion of the class name following the last
  269. underscore character, or the full class name otherwise. As
  270. an example, if the class is named
  271. <classname>Zend_Controller_Action_Helper_Redirector</classname>, it
  272. will return <emphasis>Redirector</emphasis>; a class named
  273. <emphasis>FooMessage</emphasis> will simply return itself.
  274. </para>
  275. </listitem>
  276. </itemizedlist>
  277. <para>
  278. You may optionally include a <methodname>direct()</methodname> method in your
  279. helper class. If defined, it allows you to treat the helper as a
  280. method of the helper broker, in order to allow easy, one-off usage
  281. of the helper. As an example, the <link
  282. linkend="zend.controller.actionhelpers.redirector">redirector</link>
  283. defines <methodname>direct()</methodname> as an alias of
  284. <methodname>goto()</methodname>, allowing use of the helper like this:
  285. </para>
  286. <programlisting language="php"><![CDATA[
  287. // Redirect to /blog/view/item/id/42
  288. $this->_helper->redirector('item', 'view', 'blog', array('id' => 42));
  289. ]]></programlisting>
  290. <para>
  291. Internally, the helper broker's <methodname>__call()</methodname> method looks
  292. for a helper named <emphasis>redirector</emphasis>, then checks to see if
  293. that helper has a defined <methodname>direct()</methodname> method, and calls it
  294. with the arguments provided.
  295. </para>
  296. <para>
  297. Once you have created your own helper class, you may provide access
  298. to it as described in the sections above.
  299. </para>
  300. </sect2>
  301. </sect1>
  302. <!--
  303. vim:se ts=4 sw=4 et:
  304. -->