Zend_Controller-Plugins-ErrorHandler.xml 12 KB

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