Zend_Controller-Exceptions.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 role="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><classname>Zend_Controller_Response_Abstract::renderExceptions()</classname></para>
  76. <para>
  77. By passing a boolean true value to this method, you tell the
  78. response object that it should render an exception message
  79. and backtrace when rendering itself. In this scenario, any
  80. exception raised by your application will be displayed. This
  81. is only recommended for non-production environments.
  82. </para>
  83. </listitem>
  84. <listitem>
  85. <para>
  86. <classname>Zend_Controller_Front::returnResponse()</classname> and
  87. <classname>Zend_Controller_Response_Abstract::isException()</classname>.
  88. </para>
  89. <para>
  90. By passing a boolean true to
  91. <classname>Zend_Controller_Front::returnResponse()</classname>,
  92. <classname>Zend_Controller_Front::dispatch()</classname> will not render the
  93. response, but instead return it. Once you have the response,
  94. you may then test to see if any exceptions were trapped using
  95. its <code>isException()</code> method, and retrieving the exceptions via
  96. the <code>getException()</code> method. As an example:
  97. </para>
  98. <programlisting role="php"><![CDATA[
  99. $front->returnResponse(true);
  100. $response = $front->dispatch();
  101. if ($response->isException()) {
  102. $exceptions = $response->getException();
  103. // handle exceptions ...
  104. } else {
  105. $response->sendHeaders();
  106. $response->outputBody();
  107. }
  108. ]]></programlisting>
  109. <para>
  110. The primary advantage this method offers over
  111. <classname>Zend_Controller_Front::throwExceptions()</classname> is to
  112. allow you to conditionally render the response after
  113. handling the exception. This will catch any exception in the
  114. controller chain, unlike the error handler plugin.
  115. </para>
  116. </listitem>
  117. </itemizedlist>
  118. </sect2>
  119. <sect2 id="zend.controller.exceptions.internal">
  120. <title>MVC Exceptions You May Encounter</title>
  121. <para>
  122. The various MVC components -- request, router, dispatcher, action
  123. controller, and response objects -- may each throw exceptions on
  124. occasion. Some exceptions may be conditionally overridden, and
  125. others are used to indicate the developer may need to consider
  126. their application structure.
  127. </para>
  128. <para>As some examples:</para>
  129. <itemizedlist>
  130. <listitem>
  131. <para>
  132. <classname>Zend_Controller_Dispatcher::dispatch()</classname> will,
  133. by default, throw an exception if an invalid controller is
  134. requested. There are two recommended ways to deal with
  135. this.
  136. </para>
  137. <itemizedlist>
  138. <listitem>
  139. <para>Set the <code>useDefaultControllerAlways</code> parameter.</para>
  140. <para>
  141. In your front controller, or your dispatcher, add
  142. the following directive:
  143. </para>
  144. <programlisting role="php"><![CDATA[
  145. $front->setParam('useDefaultControllerAlways', true);
  146. // or
  147. $dispatcher->setParam('useDefaultControllerAlways', true);
  148. ]]></programlisting>
  149. <para>
  150. When this flag is set, the dispatcher will use the
  151. default controller and action instead of throwing an
  152. exception. The disadvantage to this method is that
  153. any typos a user makes when accessing your site will
  154. still resolve and display your home page, which can
  155. wreak havoc with search engine optimization.
  156. </para>
  157. </listitem>
  158. <listitem>
  159. <para>
  160. The exception thrown by <code>dispatch()</code> is
  161. a <classname>Zend_Controller_Dispatcher_Exception</classname>
  162. containing the text 'Invalid controller specified'.
  163. Use one of the methods outlined in <link
  164. linkend="zend.controller.exceptions.handling">the
  165. previous section</link> to catch the exception,
  166. and then redirect to a generic error page or the
  167. home page.
  168. </para>
  169. </listitem>
  170. </itemizedlist>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. <classname>Zend_Controller_Action::__call()</classname> will throw a
  175. <classname>Zend_Controller_Action_Exception</classname> if it cannot
  176. dispatch a non-existent action to a method. Most likely,
  177. you will want to use some default action in the controller
  178. in cases like this. Ways to achieve this include:
  179. </para>
  180. <itemizedlist>
  181. <listitem>
  182. <para>
  183. Subclass <classname>Zend_Controller_Action</classname> and
  184. override the <code>__call()</code> method. As an
  185. example:
  186. </para>
  187. <programlisting role="php"><![CDATA[
  188. class My_Controller_Action extends Zend_Controller_Action
  189. {
  190. public function __call($method, $args)
  191. {
  192. if ('Action' == substr($method, -6)) {
  193. $controller = $this->getRequest()->getControllerName();
  194. $url = '/' . $controller . '/index';
  195. return $this->_redirect($url);
  196. }
  197. throw new Exception('Invalid method');
  198. }
  199. }
  200. ]]></programlisting>
  201. <para>
  202. The example above intercepts any undefined action
  203. method called and redirects it to the default action
  204. in the controller.
  205. </para>
  206. </listitem>
  207. <listitem>
  208. <para>
  209. Subclass <classname>Zend_Controller_Dispatcher</classname>
  210. and override the <code>getAction()</code> method to
  211. verify the action exists. As an example:
  212. </para>
  213. <programlisting role="php"><![CDATA[
  214. class My_Controller_Dispatcher extends Zend_Controller_Dispatcher
  215. {
  216. public function getAction($request)
  217. {
  218. $action = $request->getActionName();
  219. if (empty($action)) {
  220. $action = $this->getDefaultAction();
  221. $request->setActionName($action);
  222. $action = $this->formatActionName($action);
  223. } else {
  224. $controller = $this->getController();
  225. $action = $this->formatActionName($action);
  226. if (!method_exists($controller, $action)) {
  227. $action = $this->getDefaultAction();
  228. $request->setActionName($action);
  229. $action = $this->formatActionName($action);
  230. }
  231. }
  232. return $action;
  233. }
  234. }
  235. ]]></programlisting>
  236. <para>
  237. The above code checks to see that the requested
  238. action exists in the controller class; if not, it
  239. resets the action to the default action.
  240. </para>
  241. <para>
  242. This method is nice because you can transparently
  243. alter the action prior to final dispatch. However,
  244. it also means that typos in the URL may still
  245. dispatch correctly, which is not great for search
  246. engine optimization.
  247. </para>
  248. </listitem>
  249. <listitem>
  250. <para>
  251. Use
  252. <classname>Zend_Controller_Action::preDispatch()</classname>
  253. or
  254. <classname>Zend_Controller_Plugin_Abstract::preDispatch()</classname>
  255. to identify invalid actions.
  256. </para>
  257. <para>
  258. By subclassing <classname>Zend_Controller_Action</classname>
  259. and modifying <code>preDispatch()</code>, you can
  260. modify all of your controllers to forward to
  261. another action or redirect prior to actually
  262. dispatching the action. The code for this will look
  263. similar to the code for overriding
  264. <code>__call()</code>, above.
  265. </para>
  266. <para>
  267. Alternatively, you can check this information in a
  268. global plugin. This has the advantage of being
  269. action controller independent; if your application
  270. consists of a variety of action controllers, and not
  271. all of them inherit from the same class, this method
  272. can add consistency in handling your various
  273. classes.
  274. </para>
  275. <para>
  276. As an example:
  277. </para>
  278. <programlisting role="php"><![CDATA[
  279. class My_Controller_PreDispatchPlugin extends Zend_Controller_Plugin_Abstract
  280. {
  281. public function preDispatch(Zend_Controller_Request_Abstract $request)
  282. {
  283. $front = Zend_Controller_Front::getInstance();
  284. $dispatcher = $front->getDispatcher();
  285. $class = $dispatcher->getControllerClass($request);
  286. if (!$controller) {
  287. $class = $dispatcher->getDefaultControllerClass($request);
  288. }
  289. $r = new ReflectionClass($class);
  290. $action = $dispatcher->getActionMethod($request);
  291. if (!$r->hasMethod($action)) {
  292. $defaultAction = $dispatcher->getDefaultAction();
  293. $controllerName = $request->getControllerName();
  294. $response = $front->getResponse();
  295. $response->setRedirect('/' . $controllerName
  296. . '/' . $defaultAction);
  297. $response->sendHeaders();
  298. exit;
  299. }
  300. }
  301. }
  302. ]]></programlisting>
  303. <para>
  304. In this example, we check to see if the action
  305. requested is available in the controller. If not, we
  306. redirect to the default action in the controller,
  307. and exit script execution immediately.
  308. </para>
  309. </listitem>
  310. </itemizedlist>
  311. </listitem>
  312. </itemizedlist>
  313. </sect2>
  314. </sect1>
  315. <!--
  316. vim:se ts=4 sw=4 et:
  317. -->