Zend_Controller-Response.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. <methodname>Zend_Controller_Front::throwExceptions(true)</methodname>:
  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. <methodname>sendResponse()</methodname>.
  23. </para>
  24. <programlisting language="php"><![CDATA[
  25. $response->sendResponse();
  26. ]]></programlisting>
  27. <note>
  28. <para>
  29. By default, the front controller calls <methodname>sendResponse()</methodname>
  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 <property>returnResponse</property> flag with
  34. <methodname>Zend_Controller_Front::returnResponse(true)</methodname>:
  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. <methodname>Zend_Controller_Action::render()</methodname> does this by default.
  67. </para>
  68. </note>
  69. <para>
  70. Should an exception occur in an application, check the response object's
  71. <methodname>isException()</methodname> flag, and retrieve the exception using
  72. <methodname>getException()</methodname>. 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. <methodname>dispatch()</methodname>, or request the front controller to return the
  80. response 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 <methodname>renderExceptions()</methodname>, or enabling the
  99. front controller to <methodname>throwExceptions()</methodname>, 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 <acronym>HTTP</acronym> response headers. A variety of methods
  119. exist for this:
  120. </para>
  121. <itemizedlist>
  122. <listitem>
  123. <para>
  124. <methodname>canSendHeaders()</methodname> 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 <property>headersSentThrowsException</property> to
  129. <constant>FALSE</constant>.
  130. </para>
  131. </listitem>
  132. <listitem>
  133. <para>
  134. <methodname>setHeader($name, $value, $replace = false)</methodname> 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 <constant>TRUE</constant> will
  138. force it to do so.
  139. </para>
  140. <para>
  141. Before setting the header, it checks with
  142. <methodname>canSendHeaders()</methodname> 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. <methodname>setRedirect($url, $code = 302)</methodname> sets an
  150. <acronym>HTTP</acronym> Location header for a redirect. If an
  151. <acronym>HTTP</acronym> status code has been provided, it will use that status
  152. code.
  153. </para>
  154. <para>
  155. Internally, it calls <methodname>setHeader()</methodname> with the
  156. <varname>$replace</varname> flag on to ensure only one such header
  157. is ever sent.
  158. </para>
  159. </listitem>
  160. <listitem>
  161. <para>
  162. <methodname>getHeaders()</methodname> returns an array of all headers.
  163. Each array element is an array with the keys 'name' and
  164. 'value'.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>
  169. <methodname>clearHeaders()</methodname> clears all registered headers.
  170. </para>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. <methodname>setRawHeader()</methodname> can be used to set headers that
  175. are not key and value pairs, such as an <acronym>HTTP</acronym> status header.
  176. </para>
  177. </listitem>
  178. <listitem>
  179. <para>
  180. <methodname>getRawHeaders()</methodname> returns any registered raw
  181. headers.
  182. </para>
  183. </listitem>
  184. <listitem>
  185. <para>
  186. <methodname>clearRawHeaders()</methodname> clears any registered raw
  187. headers.
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. <methodname>clearAllHeaders()</methodname> clears both regular key and value
  193. headers as well as raw headers.
  194. </para>
  195. </listitem>
  196. </itemizedlist>
  197. <para>
  198. In addition to the above methods, there are accessors for setting
  199. and retrieving the <acronym>HTTP</acronym> response code for the current request,
  200. <methodname>setHttpResponseCode()</methodname> and
  201. <methodname>getHttpResponseCode()</methodname>.
  202. </para>
  203. </sect2>
  204. <sect2 id="zend.controller.response.namedsegments">
  205. <title>Named Segments</title>
  206. <para>
  207. The response object has support for "named segments". This allows
  208. you to segregate body content into different segments and order
  209. those segments so output is returned in a specific order.
  210. Internally, body content is saved as an array, and the various
  211. accessor methods can be used to indicate placement and names within
  212. that array.
  213. </para>
  214. <para>
  215. As an example, you could use a <methodname>preDispatch()</methodname> hook to
  216. add a header to the response object, then have the action controller
  217. add body content, and a <methodname>postDispatch()</methodname> hook add a
  218. footer:
  219. </para>
  220. <programlisting language="php"><![CDATA[
  221. // Assume that this plugin class is registered with the front controller
  222. class MyPlugin extends Zend_Controller_Plugin_Abstract
  223. {
  224. public function preDispatch(Zend_Controller_Request_Abstract $request)
  225. {
  226. $response = $this->getResponse();
  227. $view = new Zend_View();
  228. $view->setBasePath('../views/scripts');
  229. $response->prepend('header', $view->render('header.phtml'));
  230. }
  231. public function postDispatch(Zend_Controller_Request_Abstract $request)
  232. {
  233. $response = $this->getResponse();
  234. $view = new Zend_View();
  235. $view->setBasePath('../views/scripts');
  236. $response->append('footer', $view->render('footer.phtml'));
  237. }
  238. }
  239. // a sample action controller
  240. class MyController extends Zend_Controller_Action
  241. {
  242. public function fooAction()
  243. {
  244. $this->render();
  245. }
  246. }
  247. ]]></programlisting>
  248. <para>
  249. In the above example, a call to <filename>/my/foo</filename> will cause the
  250. final body content of the response object to have the following
  251. structure:
  252. </para>
  253. <programlisting language="php"><![CDATA[
  254. array(
  255. 'header' => ..., // header content
  256. 'default' => ..., // body content from MyController::fooAction()
  257. 'footer' => ... // footer content
  258. );
  259. ]]></programlisting>
  260. <para>
  261. When this is rendered, it will render in the order in which elements
  262. are arranged in the array.
  263. </para>
  264. <para>
  265. A variety of methods can be used to manipulate the named segments:
  266. </para>
  267. <itemizedlist>
  268. <listitem>
  269. <para>
  270. <methodname>setBody()</methodname> and <methodname>appendBody()</methodname>
  271. both allow you to pass a second value, <varname>$name</varname>,
  272. indicating a named segment. In each case, if you provide
  273. this, it will overwrite that specific named segment or
  274. create it if it does not exist (appending to the array by
  275. default). If no named segment is passed to
  276. <methodname>setBody()</methodname>, it resets the entire body content
  277. array. If no named segment is passed to <methodname>appendBody()</methodname>,
  278. the content is appended to the value in the 'default' name
  279. segment.
  280. </para>
  281. </listitem>
  282. <listitem>
  283. <para>
  284. <methodname>prepend($name, $content)</methodname> will create a segment
  285. named <varname>$name</varname> and place it at the beginning of
  286. the array. If the segment exists already, it will be removed
  287. prior to the operation (i.e., overwritten and replaced).
  288. </para>
  289. </listitem>
  290. <listitem>
  291. <para>
  292. <methodname>append($name, $content)</methodname> will create a segment
  293. named <varname>$name</varname> and place it at the end of
  294. the array. If the segment exists already, it will be removed
  295. prior to the operation (i.e., overwritten and replaced).
  296. </para>
  297. </listitem>
  298. <listitem>
  299. <para>
  300. <methodname>insert($name, $content, $parent = null, $before =
  301. false)</methodname> will create a segment named
  302. <varname>$name</varname>. If provided with a <varname>$parent</varname>
  303. segment, the new segment will be placed either before or
  304. after that segment (based on the value of
  305. <varname>$before</varname>) in the array. If the segment exists
  306. already, it will be removed prior to the operation (i.e.,
  307. overwritten and replaced).
  308. </para>
  309. </listitem>
  310. <listitem>
  311. <para>
  312. <methodname>clearBody($name = null)</methodname> will remove a single
  313. named segment if a <varname>$name</varname> is provided (and the
  314. entire array otherwise).
  315. </para>
  316. </listitem>
  317. <listitem>
  318. <para>
  319. <methodname>getBody($spec = false)</methodname> can be used to retrieve a
  320. single array segment if <varname>$spec</varname> is the name of a named
  321. segment. If <varname>$spec</varname> is <constant>FALSE</constant>, it returns
  322. a string formed by concatenating all named segments in order. If
  323. <varname>$spec</varname> is <constant>TRUE</constant>, it returns the body
  324. content array.
  325. </para>
  326. </listitem>
  327. </itemizedlist>
  328. </sect2>
  329. <sect2 id="zend.controller.response.exceptions">
  330. <title>Testing for Exceptions in the Response Object</title>
  331. <para>
  332. As mentioned earlier, by default, exceptions caught during dispatch
  333. are registered with the response object. Exceptions are registered
  334. in a stack, which allows you to keep all exceptions thrown --
  335. application exceptions, dispatch exceptions, plugin exceptions, etc.
  336. Should you wish to check for particular exceptions or to log
  337. exceptions, you'll want to use the response object's exception <acronym>API</acronym>:
  338. </para>
  339. <itemizedlist>
  340. <listitem>
  341. <para>
  342. <methodname>setException(Exception $e)</methodname> allows you to
  343. register an exception.
  344. </para>
  345. </listitem>
  346. <listitem>
  347. <para>
  348. <methodname>isException()</methodname> will tell you if an exception has
  349. been registered.
  350. </para>
  351. </listitem>
  352. <listitem>
  353. <para>
  354. <methodname>getException()</methodname> returns the entire
  355. exception stack.
  356. </para>
  357. </listitem>
  358. <listitem>
  359. <para>
  360. <methodname>hasExceptionOfType($type)</methodname> allows you to
  361. determine if an exception of a particular class is in the
  362. stack.
  363. </para>
  364. </listitem>
  365. <listitem>
  366. <para>
  367. <methodname>hasExceptionOfMessage($message)</methodname> allows you to
  368. determine if an exception with a specific message is in the
  369. stack.
  370. </para>
  371. </listitem>
  372. <listitem>
  373. <para>
  374. <methodname>hasExceptionOfCode($code)</methodname> allows you to
  375. determine if an exception with a specific code is in the
  376. stack.
  377. </para>
  378. </listitem>
  379. <listitem>
  380. <para>
  381. <methodname>getExceptionByType($type)</methodname> allows you to
  382. retrieve all exceptions of a specific class from the stack.
  383. It will return <constant>FALSE</constant> if none are found, and an array of
  384. exceptions otherwise.
  385. </para>
  386. </listitem>
  387. <listitem>
  388. <para>
  389. <methodname>getExceptionByMessage($message)</methodname> allows you to
  390. retrieve all exceptions with a specific message from the stack.
  391. It will return <constant>FALSE</constant> if none are found, and an array of
  392. exceptions otherwise.
  393. </para>
  394. </listitem>
  395. <listitem>
  396. <para>
  397. <methodname>getExceptionByCode($code)</methodname> allows you to
  398. retrieve all exceptions with a specific code from the stack.
  399. It will return <constant>FALSE</constant> if none are found, and an array of
  400. exceptions otherwise.
  401. </para>
  402. </listitem>
  403. <listitem>
  404. <para>
  405. <methodname>renderExceptions($flag)</methodname> allows you to set a
  406. flag indicating whether or not exceptions should be emitted
  407. when the response is sent.
  408. </para>
  409. </listitem>
  410. </itemizedlist>
  411. </sect2>
  412. <sect2 id="zend.controller.response.subclassing">
  413. <title>Subclassing the Response Object</title>
  414. <para>
  415. The purpose of the response object is to collect headers and content
  416. from the various actions and plugins and return them to the client;
  417. secondarily, it also collects any errors (exceptions) that occur in
  418. order to process them, return them, or hide them from the end user.
  419. </para>
  420. <para>
  421. The base response class is
  422. <classname>Zend_Controller_Response_Abstract</classname>, and any subclass you
  423. create should extend that class or one of its derivatives. The
  424. various methods available have been listed in the previous sections.
  425. </para>
  426. <para>
  427. Reasons to subclass the response object include modifying how output
  428. is returned based on the request environment (e.g., not sending
  429. headers for <acronym>CLI</acronym> or <acronym>PHP</acronym>-<acronym>GTK</acronym>
  430. requests), adding functionality to return a final view based on content stored in named
  431. segments, etc.
  432. </para>
  433. </sect2>
  434. </sect1>
  435. <!--
  436. vim:se ts=4 sw=4 et:
  437. -->