Zend_Controller-Exceptions.xml 15 KB

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