Zend_Controller-Response.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <sect1 id="zend.controller.response">
  2. <title>响应对象</title>
  3. <sect2 id="zend.controller.response.usage">
  4. <title>用法</title>
  5. <para>
  6. 响应对象逻辑上是<link linkend="zend.controller.request">请求对象</link>的搭档.目的在于收集消息体和/或消息头,因而可能返回大批的结果。另外前端控制器可能传递任何异常到响应对象,允许开发人员优美的处理异常。可以通过设置 <code>Zend_Controller_Front::throwExceptions(true)</code>覆盖这项功能:
  7. </para>
  8. <programlisting role="php"><![CDATA[
  9. $front->throwExceptions(true);
  10. ]]>
  11. </programlisting>
  12. <para>
  13. 如果要发送响应输出包括消息头,使用<code>sendResponse()</code>。
  14. </para>
  15. <programlisting role="php"><![CDATA[
  16. $response->sendResponse();
  17. ]]>
  18. </programlisting>
  19. <note>
  20. <para>
  21. 默认地,前端控制器完成分发请求后调用<code>sendResponse()</code>;一般地,你不需要调用它。但是,如果你想处理响应或者用它来测试你可以使用<code>Zend_Controller_Front::returnResponse(true)</code>设置<code>returnResponse</code> 标志覆盖默认行为:
  22. </para>
  23. <programlisting role="php"><![CDATA[
  24. $front->returnResponse(true);
  25. $response = $front->dispatch();
  26. // do some more processing, such as logging...
  27. // and then send the output:
  28. $response->sendResponse();
  29. ]]>
  30. </programlisting>
  31. </note>
  32. <para>
  33. 开发人员可以在动作控制器中使用响应对象。把结果写进响应对象,而不是直接渲染输出和发送消息头:
  34. </para>
  35. <programlisting role="php"><![CDATA[
  36. // Within an action controller action:
  37. // Set a header
  38. $this->getResponse()
  39. ->setHeader('Content-Type', 'text/html')
  40. ->appendBody($content);
  41. ]]>
  42. </programlisting>
  43. <para>
  44. 这样做,可以在显示内容之前,将所有消息头一次发送。
  45. </para>
  46. <note>
  47. <para>
  48. 如果使用动作控制器的 <link linkend="zend.controller.action.viewintegration">视图集成(view integration)</link>,你不需要在相应对象中设置渲染的视图脚本,因为<code>Zend_Controller_Action::render()</code> 默认完成了这些。
  49. </para>
  50. </note>
  51. <para>
  52. 如果程序中发生了异常,检查响应对象的<code>isException()</code> 标志,使用<code>getException()</code>获取异常。此外,可以创建定制的响应对象重定向到错误页面,记录异常消息,漂亮的格式化异常消息等。
  53. </para>
  54. <para>
  55. 在前端控制器执行dispatch()后可以获得响应对象,或者请求前端控制器返回响应对象代替渲染输出。
  56. </para>
  57. <programlisting role="php"><![CDATA[
  58. // retrieve post-dispatch:
  59. $front->dispatch();
  60. $response = $front->getResponse();
  61. if ($response->isException()) {
  62. // log, mail, etc...
  63. }
  64. // Or, have the front controller dispatch() process return it
  65. $front->returnResponse(true);
  66. $response = $front->dispatch();
  67. // do some processing...
  68. // finally, echo the response
  69. $response->sendResponse();
  70. ]]>
  71. </programlisting>
  72. <para>
  73. 默认地,异常消息是不显示的。可以通过调用<code>renderExceptions()</code>覆盖默认设置。或者启用前端控制器的throwExceptions():
  74. </para>
  75. <programlisting role="php"><![CDATA[
  76. $response->renderExceptions(true);
  77. $front->dispatch($request, $response);
  78. // or:
  79. $front->returnResponse(true);
  80. $response = $front->dispatch();
  81. $response->renderExceptions();
  82. $response->sendResponse();
  83. // or:
  84. $front->throwExceptions(true);
  85. $front->dispatch();
  86. ]]>
  87. </programlisting>
  88. </sect2>
  89. <sect2 id="zend.controller.response.headers">
  90. <title>处理消息头</title>
  91. <para>
  92. 如上文所述,响应对象的一项重要职责是收集和发出HTTP响应消息头,相应地存在大量的方法:
  93. </para>
  94. <itemizedlist>
  95. <listitem>
  96. <para>
  97. <code>canSendHeaders()</code> 用来判别消息头是否已发送,该方法带有一个可选的标志指示消息头已发出时是否抛出异常。可以通过设置<code>headersSentThrowsException</code> 属性为<code>false</code>来覆盖默认设置。
  98. </para>
  99. </listitem>
  100. <listitem>
  101. <para>
  102. <code>setHeader($name, $value, $replace = false)</code>用来设置单独的消息头。默认的不会替换已经存在的同名消息头,但可以设置<code>$replace</code> 为true强制替换.
  103. </para>
  104. <para>
  105. 设置消息头前,该方法先检查<code>canSendHeaders()</code>看操作是否允许,并请求抛出异常。
  106. </para>
  107. </listitem>
  108. <listitem>
  109. <para>
  110. <code>setRedirect($url, $code = 302)</code> 设置HTTP定位消息头准备重定向,如果提供HTTP状态码,重定向将会使用该状态码。
  111. </para>
  112. <para>
  113. 其内部调用<code>setHeader()</code>并使<code>$replace</code> 标志呈打开状态确保只发送一次定位消息头。
  114. </para>
  115. </listitem>
  116. <listitem>
  117. <para>
  118. <code>getHeaders()</code> 返回一个消息头数组,每个元素都是一个带有'name'和'value'键的数组。
  119. </para>
  120. </listitem>
  121. <listitem>
  122. <para>
  123. <code>clearHeaders()</code> 清除所有注册的键值消息头。
  124. </para>
  125. </listitem>
  126. <listitem>
  127. <para>
  128. <code>setRawHeader()</code> 设置没有键值对的原始消息头,比如HTTP状态消息头。
  129. </para>
  130. </listitem>
  131. <listitem>
  132. <para>
  133. <code>getRawHeaders()</code> 返回所有注册的原始消息头。
  134. </para>
  135. </listitem>
  136. <listitem>
  137. <para>
  138. <code>clearRawHeaders()</code>清除所有的原始消息头。
  139. </para>
  140. </listitem>
  141. <listitem>
  142. <para>
  143. <code>clearAllHeaders()</code> 清除所有的消息头,包括原始消息头和键值消息头。
  144. </para>
  145. </listitem>
  146. </itemizedlist>
  147. <para>
  148. 除了上述方法,还有获取和设置当前请求HTTP响应码的访问器, <code>setHttpResponseCode()</code> 和 <code>getHttpResponseCode()</code>.
  149. </para>
  150. </sect2>
  151. <sect2 id="zend.controller.response.namedsegments">
  152. <title>命名片段</title>
  153. <para>
  154. 相应对象支持“命名片段”。允许你将消息体分割成不同的片段,并呈一定顺序排列。因此输出的是以特定次序返回的。在其内部,主体内容被存储为一个数组,大量的访问器方法可以用来指示数组内位置和名称。
  155. </para>
  156. <para>
  157. 举例来说,你可以使用<code>preDispatch()</code> 钩子来向响应对象中加入页头,然后在动作控制器中加入主体内容,最后在<code>postDispatch()</code>钩子中加入页脚。
  158. </para>
  159. <programlisting role="php"><![CDATA[
  160. // Assume that this plugin class is registered with the front controller
  161. class MyPlugin extends Zend_Controller_Plugin_Abstract
  162. {
  163. public function preDispatch(Zend_Controller_Request_Abstract $request)
  164. {
  165. $response = $this->getResponse();
  166. $view = new Zend_View();
  167. $view->setBasePath('../views/scripts');
  168. $response->prepend('header', $view->render('header.phtml'));
  169. }
  170. public function postDispatch(Zend_Controller_Request_Abstract $request)
  171. {
  172. $response = $this->getResponse();
  173. $view = new Zend_View();
  174. $view->setBasePath('../views/scripts');
  175. $response->append('footer', $view->render('footer.phtml'));
  176. }
  177. }
  178. // a sample action controller
  179. class MyController extends Zend_Controller_Action
  180. {
  181. public function fooAction()
  182. {
  183. $this->render();
  184. }
  185. }
  186. ]]>
  187. </programlisting>
  188. <para>
  189. 上面的例子中,调用<code>/my/foo</code>会使得最终响应对象中的内容呈现下面的结构:
  190. </para>
  191. <programlisting role="php"><![CDATA[
  192. array(
  193. 'header' => ..., // header content
  194. 'default' => ..., // body content from MyController::fooAction()
  195. 'footer' => ... // footer content
  196. );
  197. ]]>
  198. </programlisting>
  199. <para>
  200. 渲染响应时,会按照数组中元素顺序来渲染。
  201. </para>
  202. <para>
  203. 大量的方法可以用来处理命名片段:
  204. </para>
  205. <itemizedlist>
  206. <listitem>
  207. <para>
  208. <code>setBody()</code> 和 <code>appendBody()</code> 都允许传入一个<code>$name</code>参数,指示一个命名片段。如果提供了这个参数,将会覆盖指定的命名片段,如果该片段不存在就创建一个。如果没有传入<code>$name</code>参数到<code>setBody()</code>,将会重置整个主体内容。如果没有传入<code>$name</code>参数到<code>appendBody()</code>,内容被附加到'default'命名片段。
  209. </para>
  210. </listitem>
  211. <listitem>
  212. <para>
  213. <code>prepend($name, $content)</code> 将创建一个<code>$name</code>命名片段并放置在数组的开始位置。如果该片段存在,将首先移除。
  214. </para>
  215. </listitem>
  216. <listitem>
  217. <para>
  218. <code>append($name, $content)</code> 将创建一个<code>$name</code>命名片段,并放置在数组的结尾位置。 如果该片段存在,将首先移除。
  219. </para>
  220. </listitem>
  221. <listitem>
  222. <para>
  223. <code>insert($name, $content, $parent = null, $before = false)</code> 将创建一个<code>$name</code>命名片段。如果提供<code>$parent</code>参数,新的片段视<code>$before</code>的值决定放置在 <code>$parent</code>的前面或者后面。如果该片段存在,将首先移除。
  224. </para>
  225. </listitem>
  226. <listitem>
  227. <para>
  228. <code>clearBody($name = null)</code> 如果<code>$name</code>参数提供,将删除该片段,否则删除全部。
  229. </para>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. <code>getBody($spec = false)</code> 如果<code>$spec</code>参数为一个片段名称,将可以获取到该字段。若<code>$spec</code>参数为false,将返回字符串格式的命名片段顺序链。如果<code>$spec</code>参数为true,返回主体内容数组。
  234. </para>
  235. </listitem>
  236. </itemizedlist>
  237. </sect2>
  238. <sect2 id="zend.controller.response.exceptions">
  239. <title>在响应对象中测试异常</title>
  240. <para>
  241. 如上文所述,默认的,分发过程中的异常发生会在响应对象中注册。异常会注册在一个堆中,允许你抛出所有异常--程序异常,分发异常,插件异常等。如果你要检查或者记录特定的异常,你可能想要使用响应对象的异常API:
  242. </para>
  243. <itemizedlist>
  244. <listitem>
  245. <para>
  246. <code>setException(Exception $e)</code> 注册一个异常。
  247. </para>
  248. </listitem>
  249. <listitem>
  250. <para>
  251. <code>isException()</code> 判断该异常是否注册。
  252. </para>
  253. </listitem>
  254. <listitem>
  255. <para>
  256. <code>getException()</code> 返回整个异常堆。
  257. </para>
  258. </listitem>
  259. <listitem>
  260. <para>
  261. <code>hasExceptionOfType($type)</code> 判断特定类的异常是否在堆中。
  262. </para>
  263. </listitem>
  264. <listitem>
  265. <para>
  266. <code>hasExceptionOfMessage($message)</code> 判断带有指定消息的异常是否在堆中。
  267. </para>
  268. </listitem>
  269. <listitem>
  270. <para>
  271. <code>hasExceptionOfCode($code)</code> 判断带有指定代码的异常是否在堆中。
  272. </para>
  273. </listitem>
  274. <listitem>
  275. <para>
  276. <code>getExceptionByType($type)</code> 获取堆中特定类的所有异常。如果没有则返回false,否则返回数组。
  277. </para>
  278. </listitem>
  279. <listitem>
  280. <para>
  281. <code>getExceptionByMessage($message)</code> 获取堆中带有特定消息的所有异常。如果没有则返回false,否则返回数组。
  282. </para>
  283. </listitem>
  284. <listitem>
  285. <para>
  286. <code>getExceptionByCode($code)</code> 获取堆中带有特定编码的所有异常。如果没有则返回false,否则返回数组。
  287. </para>
  288. </listitem>
  289. <listitem>
  290. <para>
  291. <code>renderExceptions($flag)</code> 设置标志指示当发送响应时是否发送其中的异常。
  292. </para>
  293. </listitem>
  294. </itemizedlist>
  295. </sect2>
  296. <sect2 id="zend.controller.response.subclassing">
  297. <title>子类化响应对象</title>
  298. <para>
  299. 响应对象的目的首先在于从大量的动作和插件中收集消息头和内容,然后返回到客户端;其次,响应对象也收集发生的任何异常,以处理或者返回这些异常,再或者对终端用户隐藏它们。
  300. </para>
  301. <para>
  302. 响应的基类是<code>Zend_Controller_Response_Abstract</code>,创建的任何子类必须继承这个类或它的衍生类。前面的章节中已经列出了大量可用的方法。
  303. </para>
  304. <para>
  305. 子类化响应对象的原因包括基于请求环境修改返回的内容的输出方式(例如:在CLI和PHP-GTK请求中不发送消息头)增加返回存储在命名片段中内容的最终视图的功能等等。
  306. </para>
  307. </sect2>
  308. </sect1>
  309. <!--
  310. vim:se ts=4 sw=4 et:
  311. -->