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