2
0

Zend_Controller-Plugins-ErrorHandler.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.plugins.standard.errorhandler">
  4. <title>Zend_Controller_Plugin_ErrorHandler</title>
  5. <para>
  6. <classname>Zend_Controller_Plugin_ErrorHandler</classname> provides a drop-in
  7. plugin for handling exceptions thrown by your application, including
  8. those resulting from missing controllers or actions; it is an
  9. alternative to the methods listed in the <link
  10. linkend="zend.controller.exceptions">MVC Exceptions section</link>.
  11. </para>
  12. <para>
  13. The primary targets of the plugin are:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>Intercept exceptions raised due to missing controllers or
  18. action methods</para>
  19. </listitem>
  20. <listitem>
  21. <para>Intercept exceptions raised within action controllers</para>
  22. </listitem>
  23. </itemizedlist>
  24. <para>
  25. In other words, the <code>ErrorHandler</code> plugin is designed to
  26. handle HTTP 404-type errors (page missing) and 500-type errors (internal
  27. error). It is not intended to catch exceptions raised in other plugins
  28. or routing.
  29. </para>
  30. <para>
  31. By default, <classname>Zend_Controller_Plugin_ErrorHandler</classname> will
  32. forward to <code>ErrorController::errorAction()</code> in the default
  33. module. You may set alternate values for these by using the various
  34. accessors available to the plugin:
  35. </para>
  36. <itemizedlist>
  37. <listitem>
  38. <para>
  39. <code>setErrorHandlerModule()</code> sets the controller module
  40. to use.
  41. </para>
  42. </listitem>
  43. <listitem>
  44. <para>
  45. <code>setErrorHandlerController()</code> sets the controller
  46. to use.
  47. </para>
  48. </listitem>
  49. <listitem>
  50. <para>
  51. <code>setErrorHandlerAction()</code> sets the controller action
  52. to use.
  53. </para>
  54. </listitem>
  55. <listitem>
  56. <para>
  57. <code>setErrorHandler()</code> takes an associative array, which
  58. may contain any of the keys 'module', 'controller', or 'action',
  59. with which it will set the appropriate values.
  60. </para>
  61. </listitem>
  62. </itemizedlist>
  63. <para>
  64. Additionally, you may pass an optional associative array to the
  65. constructor, which will then proxy to <code>setErrorHandler()</code>.
  66. </para>
  67. <para>
  68. <classname>Zend_Controller_Plugin_ErrorHandler</classname> registers a
  69. <code>postDispatch()</code> hook and checks for exceptions registered in
  70. <link linkend="zend.controller.response">the response object</link>. If
  71. any are found, it attempts to forward to the registered error handler
  72. action.
  73. </para>
  74. <para>
  75. If an exception occurs dispatching the error handler, the plugin will
  76. tell the front controller to throw exceptions, and rethrow the last
  77. exception registered with the response object.
  78. </para>
  79. <sect4 id="zend.controller.plugins.standard.errorhandler.fourohfour">
  80. <title>Using the ErrorHandler as a 404 Handler</title>
  81. <para>
  82. Since the <code>ErrorHandler</code> plugin captures not only
  83. application errors, but also errors in the controller chain arising
  84. from missing controller classes and/or action methods, it can be
  85. used as a 404 handler. To do so, you will need to have your error
  86. controller check the exception type.
  87. </para>
  88. <para>
  89. Exceptions captured are logged in an object registered in the
  90. request. To retrieve it, use
  91. <classname>Zend_Controller_Action::_getParam('error_handler')</classname>:
  92. </para>
  93. <programlisting language="php"><![CDATA[
  94. class ErrorController extends Zend_Controller_Action
  95. {
  96. public function errorAction()
  97. {
  98. $errors = $this->_getParam('error_handler');
  99. }
  100. }
  101. ]]></programlisting>
  102. <para>
  103. Once you have the error object, you can get the type via
  104. <code>$errors->type</code>. It will be one of the following:
  105. </para>
  106. <itemizedlist>
  107. <listitem>
  108. <para>
  109. <classname>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER</classname>,
  110. indicating the controller was not found.
  111. </para>
  112. </listitem>
  113. <listitem>
  114. <para>
  115. <classname>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION</classname>,
  116. indicating the requested action was not found.
  117. </para>
  118. </listitem>
  119. <listitem>
  120. <para>
  121. <classname>Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER</classname>,
  122. indicating other exceptions.
  123. </para>
  124. </listitem>
  125. </itemizedlist>
  126. <para>
  127. You can then test for either of the first two types, and, if so,
  128. indicate a 404 page:
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. class ErrorController extends Zend_Controller_Action
  132. {
  133. public function errorAction()
  134. {
  135. $errors = $this->_getParam('error_handler');
  136. switch ($errors->type) {
  137. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  138. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  139. // 404 error -- controller or action not found
  140. $this->getResponse()
  141. ->setRawHeader('HTTP/1.1 404 Not Found');
  142. // ... get some output to display...
  143. break;
  144. default:
  145. // application error; display error page, but don't
  146. // change status code
  147. break;
  148. }
  149. }
  150. }
  151. ]]></programlisting>
  152. <para>
  153. Finally, you can retrieve the exception that triggered the error
  154. handler by grabbing the <code>exception</code> property of the
  155. <code>error_handler</code> object:
  156. </para>
  157. <programlisting language="php"><![CDATA[
  158. public function errorAction()
  159. {
  160. $errors = $this->_getParam('error_handler');
  161. switch ($errors->type) {
  162. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  163. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  164. // 404 error -- controller or action not found
  165. $this->getResponse()
  166. ->setRawHeader('HTTP/1.1 404 Not Found');
  167. // ... get some output to display...
  168. break;
  169. default:
  170. // application error; display error page, but don't change
  171. // status code
  172. // ...
  173. // Log the exception:
  174. $exception = $errors->exception;
  175. $log = new Zend_Log(
  176. new Zend_Log_Writer_Stream(
  177. '/tmp/applicationException.log'
  178. )
  179. );
  180. $log->debug($exception->getMessage() . "\n" .
  181. $exception->getTraceAsString());
  182. break;
  183. }
  184. }
  185. ]]></programlisting>
  186. </sect4>
  187. <sect4 id="zend.controller.plugins.standard.errorhandler.buffer">
  188. <title>Handling Previously Rendered Output</title>
  189. <para>
  190. If you dispatch multiple actions in a request, or if your action
  191. makes multiple calls to <code>render()</code>, it's possible that the
  192. response object already has content stored within it. This can lead
  193. to rendering a mixture of expected content and error content.
  194. </para>
  195. <para>
  196. If you wish to render errors inline in such pages, no changes will
  197. be necessary. If you do not wish to render such content, you should
  198. clear the response body prior to rendering any views:
  199. </para>
  200. <programlisting language="php"><![CDATA[
  201. $this->getResponse()->clearBody();
  202. ]]></programlisting>
  203. </sect4>
  204. <sect4 id="zend.controller.plugins.standard.errorhandler.examples">
  205. <title>Plugin Usage Examples</title>
  206. <example id="zend.controller.plugins.standard.errorhandler.examples.example-1">
  207. <title>Standard Usage</title>
  208. <programlisting language="php"><![CDATA[
  209. $front = Zend_Controller_Front::getInstance();
  210. $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler());
  211. ]]></programlisting>
  212. </example>
  213. <example id="zend.controller.plugins.standard.errorhandler.examples.example-2">
  214. <title>Setting a Different Error Handler</title>
  215. <programlisting language="php"><![CDATA[
  216. $front = Zend_Controller_Front::getInstance();
  217. $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
  218. 'module' => 'mystuff',
  219. 'controller' => 'static',
  220. 'action' => 'error'
  221. )));
  222. ]]></programlisting>
  223. </example>
  224. <example id="zend.controller.plugins.standard.errorhandler.examples.example-3">
  225. <title>Using Accessors</title>
  226. <programlisting language="php"><![CDATA[
  227. $plugin = new Zend_Controller_Plugin_ErrorHandler();
  228. $plugin->setErrorHandlerModule('mystuff')
  229. ->setErrorHandlerController('static')
  230. ->setErrorHandlerAction('error');
  231. $front = Zend_Controller_Front::getInstance();
  232. $front->registerPlugin($plugin);
  233. ]]></programlisting>
  234. </example>
  235. </sect4>
  236. <sect4 id="zend.controller.plugins.standard.errorhandler.controllerexamples">
  237. <title>Error Controller Example</title>
  238. <para>
  239. In order to use the Error Handler plugin, you need an error
  240. controller. Below is a simple example.
  241. </para>
  242. <programlisting language="php"><![CDATA[
  243. class ErrorController extends Zend_Controller_Action
  244. {
  245. public function errorAction()
  246. {
  247. $errors = $this->_getParam('error_handler');
  248. switch ($errors->type) {
  249. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  250. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  251. // 404 error -- controller or action not found
  252. $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
  253. $content =<<<EOH
  254. <h1>Error!</h1>
  255. <p>The page you requested was not found.</p>
  256. EOH;
  257. break;
  258. default:
  259. // application error
  260. $content =<<<EOH
  261. <h1>Error!</h1>
  262. <p>An unexpected error occurred. Please try again later.</p>
  263. EOH;
  264. break;
  265. }
  266. // Clear previous content
  267. $this->getResponse()->clearBody();
  268. $this->view->content = $content;
  269. }
  270. }
  271. ]]></programlisting>
  272. </sect4>
  273. </sect3>
  274. <!--
  275. vim:se ts=4 sw=4 et:
  276. -->