Zend_Controller-Response.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.response">
  4. <title>The Response Object</title>
  5. <sect2 id="zend.controller.response.usage">
  6. <title>Usage</title>
  7. <para>
  8. The response object is the logical counterpart to the <link
  9. linkend="zend.controller.request">request object</link>. Its
  10. purpose is to collate content and/or headers so that they may be
  11. returned en masse. Additionally, the front controller will pass any
  12. caught exceptions to the response object, allowing the developer to
  13. gracefully handle exceptions. This functionality may be overridden
  14. by setting
  15. <classname>Zend_Controller_Front::throwExceptions(true)</classname>:
  16. </para>
  17. <programlisting language="php"><![CDATA[
  18. $front->throwExceptions(true);
  19. ]]></programlisting>
  20. <para>
  21. To send the response output, including headers, use
  22. <code>sendResponse()</code>.
  23. </para>
  24. <programlisting language="php"><![CDATA[
  25. $response->sendResponse();
  26. ]]></programlisting>
  27. <note>
  28. <para>
  29. By default, the front controller calls <code>sendResponse()</code>
  30. when it has finished dispatching the request; typically you will
  31. never need to call it. However, if you wish to manipulate the
  32. response or use it in testing, you can override this
  33. behaviour by setting the <code>returnResponse</code> flag with
  34. <classname>Zend_Controller_Front::returnResponse(true)</classname>:
  35. </para>
  36. <programlisting language="php"><![CDATA[
  37. $front->returnResponse(true);
  38. $response = $front->dispatch();
  39. // do some more processing, such as logging...
  40. // and then send the output:
  41. $response->sendResponse();
  42. ]]></programlisting>
  43. </note>
  44. <para>
  45. Developers should make use of the response object in their action
  46. controllers. Instead of directly rendering output and sending
  47. headers, push them to the response object:
  48. </para>
  49. <programlisting language="php"><![CDATA[
  50. // Within an action controller action:
  51. // Set a header
  52. $this->getResponse()
  53. ->setHeader('Content-Type', 'text/html')
  54. ->appendBody($content);
  55. ]]></programlisting>
  56. <para>
  57. By doing this, all headers get sent at once, just prior to
  58. displaying the content.
  59. </para>
  60. <note>
  61. <para>
  62. If using the action controller <link
  63. linkend="zend.controller.action.viewintegration">view
  64. integration</link>, you do not need to set the rendered view
  65. script content in the response object, as
  66. <classname>Zend_Controller_Action::render()</classname> does this by default.
  67. </para>
  68. </note>
  69. <para>
  70. Should an exception occur in an application, check the response object's
  71. <code>isException()</code> flag, and retrieve the exception using
  72. <code>getException()</code>. Additionally, one may create custom
  73. response objects that redirect to error pages, log exception messages,
  74. do pretty formatting of exception messages (for development
  75. environments), etc.
  76. </para>
  77. <para>
  78. You may retrieve the response object following the front controller
  79. dispatch(), or request the front controller to return the response
  80. object instead of rendering output.
  81. </para>
  82. <programlisting language="php"><![CDATA[
  83. // retrieve post-dispatch:
  84. $front->dispatch();
  85. $response = $front->getResponse();
  86. if ($response->isException()) {
  87. // log, mail, etc...
  88. }
  89. // Or, have the front controller dispatch() process return it
  90. $front->returnResponse(true);
  91. $response = $front->dispatch();
  92. // do some processing...
  93. // finally, echo the response
  94. $response->sendResponse();
  95. ]]></programlisting>
  96. <para>
  97. By default, exception messages are not displayed. This behaviour may be
  98. overridden by calling <code>renderExceptions()</code>, or enabling the
  99. front controller to throwExceptions(), as shown above:
  100. </para>
  101. <programlisting language="php"><![CDATA[
  102. $response->renderExceptions(true);
  103. $front->dispatch($request, $response);
  104. // or:
  105. $front->returnResponse(true);
  106. $response = $front->dispatch();
  107. $response->renderExceptions();
  108. $response->sendResponse();
  109. // or:
  110. $front->throwExceptions(true);
  111. $front->dispatch();
  112. ]]></programlisting>
  113. </sect2>
  114. <sect2 id="zend.controller.response.headers">
  115. <title>Manipulating Headers</title>
  116. <para>
  117. As stated previously, one aspect of the response object's duties is
  118. to collect and emit HTTP response headers. A variety of methods
  119. exist for this:
  120. </para>
  121. <itemizedlist>
  122. <listitem>
  123. <para>
  124. <code>canSendHeaders()</code> is used to determine if
  125. headers have already been sent. It takes an optional flag
  126. indicating whether or not to throw an exception if headers
  127. have already been sent. This can be overridden by setting
  128. the property <code>headersSentThrowsException</code> to
  129. <constant>FALSE</constant>.
  130. </para>
  131. </listitem>
  132. <listitem>
  133. <para>
  134. <code>setHeader($name, $value, $replace = false)</code> is
  135. used to set an individual header. By default, it does not
  136. replace existing headers of the same name in the object;
  137. however, setting <varname>$replace</varname> to true will force it
  138. to do so.
  139. </para>
  140. <para>
  141. Before setting the header, it checks with
  142. <code>canSendHeaders()</code> to see if this operation is
  143. allowed at this point, and requests that an exception be
  144. thrown.
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>
  149. <code>setRedirect($url, $code = 302)</code> sets an HTTP
  150. Location header for a redirect. If an HTTP status code has
  151. been provided, it will use that status code.
  152. </para>
  153. <para>
  154. Internally, it calls <code>setHeader()</code> with the
  155. <varname>$replace</varname> flag on to ensure only one such header
  156. is ever sent.
  157. </para>
  158. </listitem>
  159. <listitem>
  160. <para>
  161. <code>getHeaders()</code> returns an array of all headers.
  162. Each array element is an array with the keys 'name' and
  163. 'value'.
  164. </para>
  165. </listitem>
  166. <listitem>
  167. <para>
  168. <code>clearHeaders()</code> clears all registered headers.
  169. </para>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. <code>setRawHeader()</code> can be used to set headers that
  174. are not key/value pairs, such as an HTTP status header.
  175. </para>
  176. </listitem>
  177. <listitem>
  178. <para>
  179. <code>getRawHeaders()</code> returns any registered raw
  180. headers.
  181. </para>
  182. </listitem>
  183. <listitem>
  184. <para>
  185. <code>clearRawHeaders()</code> clears any registered raw
  186. headers.
  187. </para>
  188. </listitem>
  189. <listitem>
  190. <para>
  191. <code>clearAllHeaders()</code> clears both regular key/value
  192. headers as well as raw headers.
  193. </para>
  194. </listitem>
  195. </itemizedlist>
  196. <para>
  197. In addition to the above methods, there are accessors for setting
  198. and retrieving the HTTP response code for the current request,
  199. <code>setHttpResponseCode()</code> and
  200. <code>getHttpResponseCode()</code>.
  201. </para>
  202. </sect2>
  203. <sect2 id="zend.controller.response.namedsegments">
  204. <title>Named Segments</title>
  205. <para>
  206. The response object has support for "named segments". This allows
  207. you to segregate body content into different segments and order
  208. those segments so output is returned in a specific order.
  209. Internally, body content is saved as an array, and the various
  210. accessor methods can be used to indicate placement and names within
  211. that array.
  212. </para>
  213. <para>
  214. As an example, you could use a <code>preDispatch()</code> hook to
  215. add a header to the response object, then have the action controller
  216. add body content, and a <code>postDispatch()</code> hook add a
  217. footer:
  218. </para>
  219. <programlisting language="php"><![CDATA[
  220. // Assume that this plugin class is registered with the front controller
  221. class MyPlugin extends Zend_Controller_Plugin_Abstract
  222. {
  223. public function preDispatch(Zend_Controller_Request_Abstract $request)
  224. {
  225. $response = $this->getResponse();
  226. $view = new Zend_View();
  227. $view->setBasePath('../views/scripts');
  228. $response->prepend('header', $view->render('header.phtml'));
  229. }
  230. public function postDispatch(Zend_Controller_Request_Abstract $request)
  231. {
  232. $response = $this->getResponse();
  233. $view = new Zend_View();
  234. $view->setBasePath('../views/scripts');
  235. $response->append('footer', $view->render('footer.phtml'));
  236. }
  237. }
  238. // a sample action controller
  239. class MyController extends Zend_Controller_Action
  240. {
  241. public function fooAction()
  242. {
  243. $this->render();
  244. }
  245. }
  246. ]]></programlisting>
  247. <para>
  248. In the above example, a call to <code>/my/foo</code> will cause the
  249. final body content of the response object to have the following
  250. structure:
  251. </para>
  252. <programlisting language="php"><![CDATA[
  253. array(
  254. 'header' => ..., // header content
  255. 'default' => ..., // body content from MyController::fooAction()
  256. 'footer' => ... // footer content
  257. );
  258. ]]></programlisting>
  259. <para>
  260. When this is rendered, it will render in the order in which elements
  261. are arranged in the array.
  262. </para>
  263. <para>
  264. A variety of methods can be used to manipulate the named segments:
  265. </para>
  266. <itemizedlist>
  267. <listitem>
  268. <para>
  269. <code>setBody()</code> and <code>appendBody()</code> both
  270. allow you to pass a second value, <varname>$name</varname>,
  271. indicating a named segment. In each case, if you provide
  272. this, it will overwrite that specific named segment or
  273. create it if it does not exist (appending to the array by
  274. default). If no named segment is passed to
  275. <code>setBody()</code>, it resets the entire body content
  276. array. If no named segment is passed to appendBody(), the
  277. content is appended to the value in the 'default' name
  278. segment.
  279. </para>
  280. </listitem>
  281. <listitem>
  282. <para>
  283. <code>prepend($name, $content)</code> will create a segment
  284. named <varname>$name</varname> and place it at the beginning of
  285. the array. If the segment exists already, it will be removed
  286. prior to the operation (i.e., overwritten and replaced).
  287. </para>
  288. </listitem>
  289. <listitem>
  290. <para>
  291. <code>append($name, $content)</code> will create a segment
  292. named <varname>$name</varname> and place it at the end of
  293. the array. If the segment exists already, it will be removed
  294. prior to the operation (i.e., overwritten and replaced).
  295. </para>
  296. </listitem>
  297. <listitem>
  298. <para>
  299. <code>insert($name, $content, $parent = null, $before =
  300. false)</code> will create a segment named
  301. <varname>$name</varname>. If provided with a <varname>$parent</varname>
  302. segment, the new segment will be placed either before or
  303. after that segment (based on the value of
  304. <varname>$before</varname>) in the array. If the segment exists
  305. already, it will be removed prior to the operation (i.e.,
  306. overwritten and replaced).
  307. </para>
  308. </listitem>
  309. <listitem>
  310. <para>
  311. <code>clearBody($name = null)</code> will remove a single
  312. named segment if a <varname>$name</varname> is provided (and the
  313. entire array otherwise).
  314. </para>
  315. </listitem>
  316. <listitem>
  317. <para>
  318. <code>getBody($spec = false)</code> can be used to retrieve a single
  319. array segment if <varname>$spec</varname> is the name of a named
  320. segment. If <varname>$spec</varname> is false, it returns a string
  321. formed by concatenating all named segments in order. If
  322. <varname>$spec</varname> is true, it returns the body content
  323. array.
  324. </para>
  325. </listitem>
  326. </itemizedlist>
  327. </sect2>
  328. <sect2 id="zend.controller.response.exceptions">
  329. <title>Testing for Exceptions in the Response Object</title>
  330. <para>
  331. As mentioned earlier, by default, exceptions caught during dispatch
  332. are registered with the response object. Exceptions are registered
  333. in a stack, which allows you to keep all exceptions thrown --
  334. application exceptions, dispatch exceptions, plugin exceptions, etc.
  335. Should you wish to check for particular exceptions or to log
  336. exceptions, you'll want to use the response object's exception API:
  337. </para>
  338. <itemizedlist>
  339. <listitem>
  340. <para>
  341. <code>setException(Exception $e)</code> allows you to
  342. register an exception.
  343. </para>
  344. </listitem>
  345. <listitem>
  346. <para>
  347. <code>isException()</code> will tell you if an exception has
  348. been registered.
  349. </para>
  350. </listitem>
  351. <listitem>
  352. <para>
  353. <code>getException()</code> returns the entire
  354. exception stack.
  355. </para>
  356. </listitem>
  357. <listitem>
  358. <para>
  359. <code>hasExceptionOfType($type)</code> allows you to
  360. determine if an exception of a particular class is in the
  361. stack.
  362. </para>
  363. </listitem>
  364. <listitem>
  365. <para>
  366. <code>hasExceptionOfMessage($message)</code> allows you to
  367. determine if an exception with a specific message is in the
  368. stack.
  369. </para>
  370. </listitem>
  371. <listitem>
  372. <para>
  373. <code>hasExceptionOfCode($code)</code> allows you to
  374. determine if an exception with a specific code is in the
  375. stack.
  376. </para>
  377. </listitem>
  378. <listitem>
  379. <para>
  380. <code>getExceptionByType($type)</code> allows you to
  381. retrieve all exceptions of a specific class from the stack.
  382. It will return false if none are found, and an array of
  383. exceptions otherwise.
  384. </para>
  385. </listitem>
  386. <listitem>
  387. <para>
  388. <code>getExceptionByMessage($message)</code> allows you to
  389. retrieve all exceptions with a specific message from the stack.
  390. It will return false if none are found, and an array of
  391. exceptions otherwise.
  392. </para>
  393. </listitem>
  394. <listitem>
  395. <para>
  396. <code>getExceptionByCode($code)</code> allows you to
  397. retrieve all exceptions with a specific code from the stack.
  398. It will return false if none are found, and an array of
  399. exceptions otherwise.
  400. </para>
  401. </listitem>
  402. <listitem>
  403. <para>
  404. <code>renderExceptions($flag)</code> allows you to set a
  405. flag indicating whether or not exceptions should be emitted
  406. when the response is sent.
  407. </para>
  408. </listitem>
  409. </itemizedlist>
  410. </sect2>
  411. <sect2 id="zend.controller.response.subclassing">
  412. <title>Subclassing the Response Object</title>
  413. <para>
  414. The purpose of the response object is to collect headers and content
  415. from the various actions and plugins and return them to the client;
  416. secondarily, it also collects any errors (exceptions) that occur in
  417. order to process them, return them, or hide them from the end user.
  418. </para>
  419. <para>
  420. The base response class is
  421. <classname>Zend_Controller_Response_Abstract</classname>, and any subclass you
  422. create should extend that class or one of its derivatives. The
  423. various methods available have been listed in the previous sections.
  424. </para>
  425. <para>
  426. Reasons to subclass the response object include modifying how output
  427. is returned based on the request environment (e.g., not sending
  428. headers for CLI or PHP-GTK requests), adding functionality to return
  429. a final view based on content stored in named segments, etc.
  430. </para>
  431. </sect2>
  432. </sect1>
  433. <!--
  434. vim:se ts=4 sw=4 et:
  435. -->