2
0

Zend_Controller-Exceptions.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.exceptions">
  4. <title>MVC Exceptions</title>
  5. <sect2 id="zend.controller.exceptions.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The MVC components in Zend Framework utilize a Front Controller,
  9. which means that all requests to a given site will go through a
  10. single entry point. As a result, all exceptions bubble up to the
  11. Front Controller eventually, allowing the developer to handle them
  12. in a single location.
  13. </para>
  14. <para>
  15. However, exception messages and backtrace information often contain
  16. sensitive system information, such as SQL statements, file
  17. locations, and more. To help protect your site, by default
  18. <classname>Zend_Controller_Front</classname> catches all exceptions and
  19. registers them with the response object; in turn, by default, the
  20. response object does not display exception messages.
  21. </para>
  22. </sect2>
  23. <sect2 id="zend.controller.exceptions.handling">
  24. <title>Handling Exceptions</title>
  25. <para>
  26. Several mechanisms are built in to the MVC components already to
  27. allow you to handle exceptions.
  28. </para>
  29. <itemizedlist>
  30. <listitem>
  31. <para>
  32. By default, the <link
  33. linkend="zend.controller.plugins.standard.errorhandler">error
  34. handler plugin</link> is registered and active. This plugin
  35. was designed to handle:
  36. </para>
  37. <itemizedlist>
  38. <listitem><para>Errors due to missing controllers or actions</para></listitem>
  39. <listitem><para>Errors occurring within action controllers</para></listitem>
  40. </itemizedlist>
  41. <para>
  42. It operates as a <code>postDispatch()</code> plugin, and
  43. checks to see if a dispatcher, action controller, or
  44. other exception has occurred. If so, it forwards to an error
  45. handler controller.
  46. </para>
  47. <para>
  48. This handler will cover most exceptional situations, and
  49. handle missing controllers and actions gracefully.
  50. </para>
  51. </listitem>
  52. <listitem>
  53. <para><classname>Zend_Controller_Front::throwExceptions()</classname></para>
  54. <para>
  55. By passing a boolean true value to this method, you can tell
  56. the front controller that instead of aggregating exceptions
  57. in the response object or using the error handler plugin,
  58. you'd rather handle them yourself. As an example:
  59. </para>
  60. <programlisting language="php"><![CDATA[
  61. $front->throwExceptions(true);
  62. try {
  63. $front->dispatch();
  64. } catch (Exception $e) {
  65. // handle exceptions yourself
  66. }
  67. ]]></programlisting>
  68. <para>
  69. This method is probably the easiest way to add custom
  70. exception handling covering the full range of possible
  71. exceptions to your front controller application.
  72. </para>
  73. </listitem>
  74. <listitem>
  75. <para>
  76. <classname>Zend_Controller_Response_Abstract::renderExceptions()</classname>
  77. </para>
  78. <para>
  79. By passing a boolean true value to this method, you tell the
  80. response object that it should render an exception message
  81. and backtrace when rendering itself. In this scenario, any
  82. exception raised by your application will be displayed. This
  83. is only recommended for non-production environments.
  84. </para>
  85. </listitem>
  86. <listitem>
  87. <para>
  88. <classname>Zend_Controller_Front::returnResponse()</classname> and
  89. <classname>Zend_Controller_Response_Abstract::isException()</classname>.
  90. </para>
  91. <para>
  92. By passing a boolean true to
  93. <classname>Zend_Controller_Front::returnResponse()</classname>,
  94. <classname>Zend_Controller_Front::dispatch()</classname> will not render the
  95. response, but instead return it. Once you have the response,
  96. you may then test to see if any exceptions were trapped using
  97. its <code>isException()</code> method, and retrieving the exceptions via
  98. the <code>getException()</code> method. As an example:
  99. </para>
  100. <programlisting language="php"><![CDATA[
  101. $front->returnResponse(true);
  102. $response = $front->dispatch();
  103. if ($response->isException()) {
  104. $exceptions = $response->getException();
  105. // handle exceptions ...
  106. } else {
  107. $response->sendHeaders();
  108. $response->outputBody();
  109. }
  110. ]]></programlisting>
  111. <para>
  112. The primary advantage this method offers over
  113. <classname>Zend_Controller_Front::throwExceptions()</classname> is to
  114. allow you to conditionally render the response after
  115. handling the exception. This will catch any exception in the
  116. controller chain, unlike the error handler plugin.
  117. </para>
  118. </listitem>
  119. </itemizedlist>
  120. </sect2>
  121. <sect2 id="zend.controller.exceptions.internal">
  122. <title>MVC Exceptions You May Encounter</title>
  123. <para>
  124. The various MVC components -- request, router, dispatcher, action
  125. controller, and response objects -- may each throw exceptions on
  126. occasion. Some exceptions may be conditionally overridden, and
  127. others are used to indicate the developer may need to consider
  128. their application structure.
  129. </para>
  130. <para>As some examples:</para>
  131. <itemizedlist>
  132. <listitem>
  133. <para>
  134. <classname>Zend_Controller_Dispatcher::dispatch()</classname> will,
  135. by default, throw an exception if an invalid controller is
  136. requested. There are two recommended ways to deal with
  137. this.
  138. </para>
  139. <itemizedlist>
  140. <listitem>
  141. <para>Set the <code>useDefaultControllerAlways</code> parameter.</para>
  142. <para>
  143. In your front controller, or your dispatcher, add
  144. the following directive:
  145. </para>
  146. <programlisting language="php"><![CDATA[
  147. $front->setParam('useDefaultControllerAlways', true);
  148. // or
  149. $dispatcher->setParam('useDefaultControllerAlways', true);
  150. ]]></programlisting>
  151. <para>
  152. When this flag is set, the dispatcher will use the
  153. default controller and action instead of throwing an
  154. exception. The disadvantage to this method is that
  155. any typos a user makes when accessing your site will
  156. still resolve and display your home page, which can
  157. wreak havoc with search engine optimization.
  158. </para>
  159. </listitem>
  160. <listitem>
  161. <para>
  162. The exception thrown by <code>dispatch()</code> is
  163. a <classname>Zend_Controller_Dispatcher_Exception</classname>
  164. containing the text 'Invalid controller specified'.
  165. Use one of the methods outlined in <link
  166. linkend="zend.controller.exceptions.handling">the
  167. previous section</link> to catch the exception,
  168. and then redirect to a generic error page or the
  169. home page.
  170. </para>
  171. </listitem>
  172. </itemizedlist>
  173. </listitem>
  174. <listitem>
  175. <para>
  176. <classname>Zend_Controller_Action::__call()</classname> will throw a
  177. <classname>Zend_Controller_Action_Exception</classname> if it cannot
  178. dispatch a non-existent action to a method. Most likely,
  179. you will want to use some default action in the controller
  180. in cases like this. Ways to achieve this include:
  181. </para>
  182. <itemizedlist>
  183. <listitem>
  184. <para>
  185. Subclass <classname>Zend_Controller_Action</classname> and
  186. override the <code>__call()</code> method. As an
  187. example:
  188. </para>
  189. <programlisting language="php"><![CDATA[
  190. class My_Controller_Action extends Zend_Controller_Action
  191. {
  192. public function __call($method, $args)
  193. {
  194. if ('Action' == substr($method, -6)) {
  195. $controller = $this->getRequest()->getControllerName();
  196. $url = '/' . $controller . '/index';
  197. return $this->_redirect($url);
  198. }
  199. throw new Exception('Invalid method');
  200. }
  201. }
  202. ]]></programlisting>
  203. <para>
  204. The example above intercepts any undefined action
  205. method called and redirects it to the default action
  206. in the controller.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. Subclass <classname>Zend_Controller_Dispatcher</classname>
  212. and override the <code>getAction()</code> method to
  213. verify the action exists. As an example:
  214. </para>
  215. <programlisting language="php"><![CDATA[
  216. class My_Controller_Dispatcher extends Zend_Controller_Dispatcher
  217. {
  218. public function getAction($request)
  219. {
  220. $action = $request->getActionName();
  221. if (empty($action)) {
  222. $action = $this->getDefaultAction();
  223. $request->setActionName($action);
  224. $action = $this->formatActionName($action);
  225. } else {
  226. $controller = $this->getController();
  227. $action = $this->formatActionName($action);
  228. if (!method_exists($controller, $action)) {
  229. $action = $this->getDefaultAction();
  230. $request->setActionName($action);
  231. $action = $this->formatActionName($action);
  232. }
  233. }
  234. return $action;
  235. }
  236. }
  237. ]]></programlisting>
  238. <para>
  239. The above code checks to see that the requested
  240. action exists in the controller class; if not, it
  241. resets the action to the default action.
  242. </para>
  243. <para>
  244. This method is nice because you can transparently
  245. alter the action prior to final dispatch. However,
  246. it also means that typos in the URL may still
  247. dispatch correctly, which is not great for search
  248. engine optimization.
  249. </para>
  250. </listitem>
  251. <listitem>
  252. <para>
  253. Use
  254. <classname>Zend_Controller_Action::preDispatch()</classname>
  255. or
  256. <classname>Zend_Controller_Plugin_Abstract::preDispatch()</classname>
  257. to identify invalid actions.
  258. </para>
  259. <para>
  260. By subclassing <classname>Zend_Controller_Action</classname>
  261. and modifying <code>preDispatch()</code>, you can
  262. modify all of your controllers to forward to
  263. another action or redirect prior to actually
  264. dispatching the action. The code for this will look
  265. similar to the code for overriding
  266. <code>__call()</code>, above.
  267. </para>
  268. <para>
  269. Alternatively, you can check this information in a
  270. global plugin. This has the advantage of being
  271. action controller independent; if your application
  272. consists of a variety of action controllers, and not
  273. all of them inherit from the same class, this method
  274. can add consistency in handling your various
  275. classes.
  276. </para>
  277. <para>
  278. As an example:
  279. </para>
  280. <programlisting language="php"><![CDATA[
  281. class My_Controller_PreDispatchPlugin extends Zend_Controller_Plugin_Abstract
  282. {
  283. public function preDispatch(Zend_Controller_Request_Abstract $request)
  284. {
  285. $front = Zend_Controller_Front::getInstance();
  286. $dispatcher = $front->getDispatcher();
  287. $class = $dispatcher->getControllerClass($request);
  288. if (!$controller) {
  289. $class = $dispatcher->getDefaultControllerClass($request);
  290. }
  291. $r = new ReflectionClass($class);
  292. $action = $dispatcher->getActionMethod($request);
  293. if (!$r->hasMethod($action)) {
  294. $defaultAction = $dispatcher->getDefaultAction();
  295. $controllerName = $request->getControllerName();
  296. $response = $front->getResponse();
  297. $response->setRedirect('/' . $controllerName
  298. . '/' . $defaultAction);
  299. $response->sendHeaders();
  300. exit;
  301. }
  302. }
  303. }
  304. ]]></programlisting>
  305. <para>
  306. In this example, we check to see if the action
  307. requested is available in the controller. If not, we
  308. redirect to the default action in the controller,
  309. and exit script execution immediately.
  310. </para>
  311. </listitem>
  312. </itemizedlist>
  313. </listitem>
  314. </itemizedlist>
  315. </sect2>
  316. </sect1>
  317. <!--
  318. vim:se ts=4 sw=4 et:
  319. -->