Zend_Controller-Response.xml 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. <sect3 id="zend.controller.response.headers.setcookie">
  204. <title>Setting Cookie Headers</title>
  205. <para>
  206. You can inject <acronym>HTTP</acronym> Set-Cookie headers into the response object
  207. of an action controller by using the provided header class
  208. <classname>Zend_Http_Header_SetCookie</classname>
  209. </para>
  210. <sect4 id="zend.controller.response.headers.setcookie.constructor">
  211. <title>Constructor Arguments</title>
  212. <para>
  213. The following table lists all constructor arguments of
  214. <classname>Zend_Http_Header_SetCookie</classname>
  215. in the order they are accepted. All arguments are optional,
  216. but name and value must be supplied via their setters if not
  217. passed in via the constructor or the resulting Set-Cookie header
  218. be invalid.
  219. </para>
  220. <itemizedlist>
  221. <listitem>
  222. <para>
  223. <varname>$name</varname>: The name of the cookie
  224. </para>
  225. </listitem>
  226. <listitem>
  227. <para>
  228. <varname>$value</varname>: The value of the cookie
  229. </para>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. <varname>$expires</varname>: The time the cookie expires
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <varname>$path</varname>: The path on the server in which
  239. the cookie will be available
  240. </para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. <varname>$domain</varname>: The domain to restrict cookie to
  245. </para>
  246. </listitem>
  247. <listitem>
  248. <para>
  249. <varname>$secure</varname>: boolean indicating whether cookie
  250. should be sent over an unencrypted connection (false) or via
  251. <acronym>HTTPS</acronym> only (true)
  252. </para>
  253. </listitem>
  254. <listitem>
  255. <para>
  256. <varname>$httpOnly</varname>: boolean indicating whether cookie
  257. should be transmitted only via the <acronym>HTTP</acronym> protocol
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <varname>$maxAge</varname>: The maximum age of the cookie in seconds
  263. </para>
  264. </listitem>
  265. <listitem>
  266. <para>
  267. <varname>$version</varname>: The cookie specification version
  268. </para>
  269. </listitem>
  270. </itemizedlist>
  271. <example>
  272. <title>Populate Zend_Http_Header_SetCookie via constructor and add to response</title>
  273. <programlisting language="php"><![CDATA[
  274. $this->getResponse()->setRawHeader(new Zend_Http_Header_SetCookie(
  275. 'foo', 'bar', NULL, '/', 'example.com', false, true
  276. ));
  277. ]]></programlisting>
  278. </example>
  279. <example>
  280. <title>Populate Zend_Http_Header_SetCookie via setters and add to response</title>
  281. <programlisting language="php"><![CDATA[
  282. $cookie = new Zend_Http_Header_SetCookie();
  283. $cookie->setName('foo')
  284. ->setValue('bar')
  285. ->setDomain('example.com')
  286. ->setPath('/')
  287. ->setHttponly(true);
  288. $this->getResponse()->setRawHeader($cookie);
  289. ]]></programlisting>
  290. </example>
  291. </sect4>
  292. </sect3>
  293. </sect2>
  294. <sect2 id="zend.controller.response.namedsegments">
  295. <title>Named Segments</title>
  296. <para>
  297. The response object has support for "named segments". This allows
  298. you to segregate body content into different segments and order
  299. those segments so output is returned in a specific order.
  300. Internally, body content is saved as an array, and the various
  301. accessor methods can be used to indicate placement and names within
  302. that array.
  303. </para>
  304. <para>
  305. As an example, you could use a <methodname>preDispatch()</methodname> hook to
  306. add a header to the response object, then have the action controller
  307. add body content, and a <methodname>postDispatch()</methodname> hook add a
  308. footer:
  309. </para>
  310. <programlisting language="php"><![CDATA[
  311. // Assume that this plugin class is registered with the front controller
  312. class MyPlugin extends Zend_Controller_Plugin_Abstract
  313. {
  314. public function preDispatch(Zend_Controller_Request_Abstract $request)
  315. {
  316. $response = $this->getResponse();
  317. $view = new Zend_View();
  318. $view->setBasePath('../views/scripts');
  319. $response->prepend('header', $view->render('header.phtml'));
  320. }
  321. public function postDispatch(Zend_Controller_Request_Abstract $request)
  322. {
  323. $response = $this->getResponse();
  324. $view = new Zend_View();
  325. $view->setBasePath('../views/scripts');
  326. $response->append('footer', $view->render('footer.phtml'));
  327. }
  328. }
  329. // a sample action controller
  330. class MyController extends Zend_Controller_Action
  331. {
  332. public function fooAction()
  333. {
  334. $this->render();
  335. }
  336. }
  337. ]]></programlisting>
  338. <para>
  339. In the above example, a call to <filename>/my/foo</filename> will cause the
  340. final body content of the response object to have the following
  341. structure:
  342. </para>
  343. <programlisting language="php"><![CDATA[
  344. array(
  345. 'header' => ..., // header content
  346. 'default' => ..., // body content from MyController::fooAction()
  347. 'footer' => ... // footer content
  348. );
  349. ]]></programlisting>
  350. <para>
  351. When this is rendered, it will render in the order in which elements
  352. are arranged in the array.
  353. </para>
  354. <para>
  355. A variety of methods can be used to manipulate the named segments:
  356. </para>
  357. <itemizedlist>
  358. <listitem>
  359. <para>
  360. <methodname>setBody()</methodname>
  361. allows you to pass a second value, <varname>$name</varname>,
  362. indicating a named segment. If you provide a segment name
  363. it will overwrite that specific named segment or
  364. create it if it does not exist (appending to the body array by
  365. default). If no named segment is passed to
  366. <methodname>setBody()</methodname>, it resets the entire body content
  367. array.
  368. </para>
  369. </listitem>
  370. <listitem>
  371. <para>
  372. <methodname>appendBody()</methodname> also allows you to pass
  373. a second value, <varname>$name</varname>, indicating a named segment.
  374. If you provide a segment name it will append the supplied content
  375. to the existing content in the named segment, or create the segment
  376. if it does not exist (appending to the body array by
  377. default). If no named segment is passed to
  378. <methodname>appendBody()</methodname>, it will append the supplied
  379. content to the named segment 'default', creating it if it does not
  380. already exist.
  381. </para>
  382. </listitem>
  383. <listitem>
  384. <para>
  385. <methodname>prepend($name, $content)</methodname> will create a segment
  386. named <varname>$name</varname> and place it at the beginning of
  387. the array. If the segment exists already, it will be removed
  388. prior to the operation (i.e., overwritten and replaced).
  389. </para>
  390. </listitem>
  391. <listitem>
  392. <para>
  393. <methodname>append($name, $content)</methodname> will create a segment
  394. named <varname>$name</varname> and place it at the end of
  395. the array. If the segment exists already, it will be removed
  396. prior to the operation (i.e., overwritten and replaced).
  397. </para>
  398. </listitem>
  399. <listitem>
  400. <para>
  401. <methodname>insert($name, $content, $parent = null, $before =
  402. false)</methodname> will create a segment named
  403. <varname>$name</varname>. If provided with a <varname>$parent</varname>
  404. segment, the new segment will be placed either before or
  405. after that segment (based on the value of
  406. <varname>$before</varname>) in the array. If the segment exists
  407. already, it will be removed prior to the operation (i.e.,
  408. overwritten and replaced).
  409. </para>
  410. </listitem>
  411. <listitem>
  412. <para>
  413. <methodname>clearBody($name = null)</methodname> will remove a single
  414. named segment if a <varname>$name</varname> is provided (and the
  415. entire array otherwise).
  416. </para>
  417. </listitem>
  418. <listitem>
  419. <para>
  420. <methodname>getBody($spec = false)</methodname> can be used to retrieve a
  421. single array segment if <varname>$spec</varname> is the name of a named
  422. segment. If <varname>$spec</varname> is <constant>FALSE</constant>, it returns
  423. a string formed by concatenating all named segments in order. If
  424. <varname>$spec</varname> is <constant>TRUE</constant>, it returns the body
  425. content array.
  426. </para>
  427. </listitem>
  428. </itemizedlist>
  429. </sect2>
  430. <sect2 id="zend.controller.response.exceptions">
  431. <title>Testing for Exceptions in the Response Object</title>
  432. <para>
  433. As mentioned earlier, by default, exceptions caught during dispatch
  434. are registered with the response object. Exceptions are registered
  435. in a stack, which allows you to keep all exceptions thrown --
  436. application exceptions, dispatch exceptions, plugin exceptions, etc.
  437. Should you wish to check for particular exceptions or to log
  438. exceptions, you'll want to use the response object's exception <acronym>API</acronym>:
  439. </para>
  440. <itemizedlist>
  441. <listitem>
  442. <para>
  443. <methodname>setException(Exception $e)</methodname> allows you to
  444. register an exception.
  445. </para>
  446. </listitem>
  447. <listitem>
  448. <para>
  449. <methodname>isException()</methodname> will tell you if an exception has
  450. been registered.
  451. </para>
  452. </listitem>
  453. <listitem>
  454. <para>
  455. <methodname>getException()</methodname> returns the entire
  456. exception stack.
  457. </para>
  458. </listitem>
  459. <listitem>
  460. <para>
  461. <methodname>hasExceptionOfType($type)</methodname> allows you to
  462. determine if an exception of a particular class is in the
  463. stack.
  464. </para>
  465. </listitem>
  466. <listitem>
  467. <para>
  468. <methodname>hasExceptionOfMessage($message)</methodname> allows you to
  469. determine if an exception with a specific message is in the
  470. stack.
  471. </para>
  472. </listitem>
  473. <listitem>
  474. <para>
  475. <methodname>hasExceptionOfCode($code)</methodname> allows you to
  476. determine if an exception with a specific code is in the
  477. stack.
  478. </para>
  479. </listitem>
  480. <listitem>
  481. <para>
  482. <methodname>getExceptionByType($type)</methodname> allows you to
  483. retrieve all exceptions of a specific class from the stack.
  484. It will return <constant>FALSE</constant> if none are found, and an array of
  485. exceptions otherwise.
  486. </para>
  487. </listitem>
  488. <listitem>
  489. <para>
  490. <methodname>getExceptionByMessage($message)</methodname> allows you to
  491. retrieve all exceptions with a specific message from the stack.
  492. It will return <constant>FALSE</constant> if none are found, and an array of
  493. exceptions otherwise.
  494. </para>
  495. </listitem>
  496. <listitem>
  497. <para>
  498. <methodname>getExceptionByCode($code)</methodname> allows you to
  499. retrieve all exceptions with a specific code from the stack.
  500. It will return <constant>FALSE</constant> if none are found, and an array of
  501. exceptions otherwise.
  502. </para>
  503. </listitem>
  504. <listitem>
  505. <para>
  506. <methodname>renderExceptions($flag)</methodname> allows you to set a
  507. flag indicating whether or not exceptions should be emitted
  508. when the response is sent.
  509. </para>
  510. </listitem>
  511. </itemizedlist>
  512. </sect2>
  513. <sect2 id="zend.controller.response.subclassing">
  514. <title>Subclassing the Response Object</title>
  515. <para>
  516. The purpose of the response object is to collect headers and content
  517. from the various actions and plugins and return them to the client;
  518. secondarily, it also collects any errors (exceptions) that occur in
  519. order to process them, return them, or hide them from the end user.
  520. </para>
  521. <para>
  522. The base response class is
  523. <classname>Zend_Controller_Response_Abstract</classname>, and any subclass you
  524. create should extend that class or one of its derivatives. The
  525. various methods available have been listed in the previous sections.
  526. </para>
  527. <para>
  528. Reasons to subclass the response object include modifying how output
  529. is returned based on the request environment (e.g., not sending
  530. headers for <acronym>CLI</acronym> or <acronym>PHP</acronym>-<acronym>GTK</acronym>
  531. requests), adding functionality to return a final view based on content stored in named
  532. segments, etc.
  533. </para>
  534. </sect2>
  535. </sect1>
  536. <!--
  537. vim:se ts=4 sw=4 et:
  538. -->