Zend_Controller-Response.xml 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24997 -->
  4. <sect1 id="zend.controller.response">
  5. <title>レスポンスオブジェクト</title>
  6. <sect2 id="zend.controller.response.usage">
  7. <title>使用法</title>
  8. <para>
  9. レスポンスオブジェクトは、
  10. <link linkend="zend.controller.request">リクエストオブジェクト</link>
  11. と対になるものです。
  12. その目的は、コンテンツやヘッダを収集し、それを返すことです。
  13. さらに、フロントコントローラで捕捉した例外はすべてレスポンスオブジェクトに渡されます。
  14. これにより、例外の処理がやりやすくなります。
  15. この挙動を変更するには
  16. <methodname>Zend_Controller_Front::throwExceptions(true)</methodname>
  17. と設定します。
  18. </para>
  19. <programlisting language="php"><![CDATA[
  20. $front->throwExceptions(true);
  21. ]]></programlisting>
  22. <para>
  23. ヘッダを含むレスポンス出力を送信するには、
  24. <methodname>sendResponse()</methodname> を使用します。
  25. </para>
  26. <programlisting language="php"><![CDATA[
  27. $response->sendResponse();
  28. ]]></programlisting>
  29. <note>
  30. <para>
  31. デフォルトでは、リクエストのディスパッチに終了した時点でフロントコントローラが
  32. <methodname>sendResponse()</methodname> をコールします。通常はこれをコールする必要はありません。
  33. しかし、テスト中などにレスポンスの内容を操作したい場合は、
  34. <property>returnResponse</property> フラグを
  35. <methodname>Zend_Controller_Front::returnResponse(true)</methodname>
  36. と設定することでこの振る舞いを変更できます。
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. $front->returnResponse(true);
  40. $response = $front->dispatch();
  41. // 何かの処理、たとえばログの記録などを行ってから
  42. // 結果を出力します
  43. $response->sendResponse();
  44. ]]></programlisting>
  45. </note>
  46. <para>
  47. 開発者は、アクションコントローラの中でレスポンスオブジェクトを使用しなければなりません。
  48. 出力を直接レンダリングしたり直接ヘッダを送信したりするのではなく、
  49. それらをレスポンスオブジェクトに格納するようにします。
  50. </para>
  51. <programlisting language="php"><![CDATA[
  52. // アクションコントローラのアクション内で、
  53. // ヘッダを設定します
  54. $this->getResponse()
  55. ->setHeader('Content-Type', 'text/html')
  56. ->appendBody($content);
  57. ]]></programlisting>
  58. <para>
  59. こうすることで、すべてのヘッダを一度に送信し、
  60. その後でコンテンツを表示することができます。
  61. </para>
  62. <note>
  63. <para>
  64. アクションコントローラで <link
  65. linkend="zend.controller.action.viewintegration">ビューの統合
  66. </link> を使用する場合は、
  67. レンダリングされたビュースクリプトの内容をレスポンスオブジェクトに設定する必要はありません。
  68. <methodname>Zend_Controller_Action::render()</methodname> がデフォルトでこれを行います。
  69. </para>
  70. </note>
  71. <para>
  72. アプリケーションで例外が発生したかどうかを調べるには、
  73. レスポンスオブジェクトの <methodname>isException()</methodname>
  74. フラグを調べます。例外を取得するには <methodname>getException()</methodname>
  75. を使用します。さらに、独自のレスポンスオブジェクトを作成して、
  76. エラーページへのリダイレクトや例外メッセージのログ出力、
  77. 例外をわかりやすく表示する (開発用) などを行うことができます。
  78. </para>
  79. <para>
  80. レスポンスオブジェクトは、フロントコントローラの
  81. <methodname>dispatch()</methodname> から受け取ることになります。あるいは、
  82. 出力のレンダリングを行わない状態のレスポンスオブジェクトを
  83. フロントコントローラから受け取ることもできます。
  84. </para>
  85. <programlisting language="php"><![CDATA[
  86. // dispatch の後に取得します
  87. $front->dispatch();
  88. $response = $front->getResponse();
  89. if ($response->isException()) {
  90. // ログへの記録、メール送信など...
  91. }
  92. // あるいは、フロントコントローラの dispatch() の返り値を使用します
  93. $front->returnResponse(true);
  94. $response = $front->dispatch();
  95. // 何かの処理...
  96. // 最後に結果を表示します
  97. $response->sendResponse();
  98. ]]></programlisting>
  99. <para>
  100. デフォルトでは、例外メッセージは表示されません。
  101. この挙動をオーバーライドするには <methodname>renderExceptions()</methodname>
  102. メソッドを使用するか、あるいは上で示したようにフロントコントローラで
  103. <methodname>throwExceptions()</methodname> を有効にします。
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. $response->renderExceptions(true);
  107. $front->dispatch($request, $response);
  108. // あるいは
  109. $front->returnResponse(true);
  110. $response = $front->dispatch();
  111. $response->renderExceptions();
  112. $response->sendResponse();
  113. // あるいは
  114. $front->throwExceptions(true);
  115. $front->dispatch();
  116. ]]></programlisting>
  117. </sect2>
  118. <sect2 id="zend.controller.response.headers">
  119. <title>ヘッダの操作</title>
  120. <para>
  121. 先ほど説明したように、レスポンスオブジェクトの役割のひとつは
  122. <acronym>HTTP</acronym> レスポンスヘッダを発行することです。
  123. このために、さまざまなメソッドが用意されています。
  124. </para>
  125. <itemizedlist>
  126. <listitem>
  127. <para>
  128. <methodname>canSendHeaders()</methodname> を使用して、
  129. ヘッダがすでに送信されているかどうかを調べます。
  130. オプションのフラグで、ヘッダが送信済みの場合に例外をスローするかどうかを指定します。
  131. この設定は、プロパティ <property>headersSentThrowsException</property>
  132. を <constant>FALSE</constant> にすることで上書きできます。
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <methodname>setHeader($name, $value, $replace = false)</methodname>
  138. を使用して、個々のヘッダを設定します。デフォルトでは、
  139. 同名のヘッダがすでに存在した場合に既存のヘッダを置換することはありません。
  140. しかし、<varname>$replace</varname> を <constant>TRUE</constant> に設定すると、
  141. 既存のヘッダを上書きするようになります。
  142. </para>
  143. <para>
  144. ヘッダを設定する前に、このメソッドは
  145. <methodname>canSendHeaders()</methodname> を使用して
  146. ヘッダが現時点で送信済みでないかどうか、例外をスローするかどうかを調べます。
  147. </para>
  148. </listitem>
  149. <listitem>
  150. <para>
  151. <methodname>setRedirect($url, $code = 302)</methodname> は、
  152. リダイレクト用の <acronym>HTTP</acronym> Location ヘッダを設定します。
  153. <acronym>HTTP</acronym> ステータスコードを指定した場合は、そのコードを使用します。
  154. </para>
  155. <para>
  156. 内部的には、このメソッドは
  157. <varname>$replace</varname> フラグをオンにして
  158. <methodname>setHeader()</methodname> をコールしています。
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <methodname>getHeaders()</methodname> は、すべてのヘッダを配列で返します。
  164. 個々の配列の要素は、'name' および 'value'
  165. のふたつのキーを持つ配列となります。
  166. </para>
  167. </listitem>
  168. <listitem>
  169. <para>
  170. <methodname>clearHeaders()</methodname> は登録済みのヘッダをすべて削除します。
  171. </para>
  172. </listitem>
  173. <listitem>
  174. <para>
  175. <methodname>setRawHeader()</methodname>
  176. を使用して、キー/値 の組になっていないヘッダを設定します。
  177. たとえば <acronym>HTTP</acronym> status ヘッダなどがこれにあたります。
  178. </para>
  179. </listitem>
  180. <listitem>
  181. <para>
  182. <methodname>getRawHeaders()</methodname> は、登録済みの生のヘッダを返します。
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. <methodname>clearRawHeaders()</methodname> は、登録済みの生のヘッダを消去します。
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. <methodname>clearAllHeaders()</methodname> は、キー/値 のペアである通常のヘッダと
  193. 生のヘッダの両方を消去します。
  194. </para>
  195. </listitem>
  196. </itemizedlist>
  197. <para>
  198. これらのメソッドのほかに、現在のリクエストの <acronym>HTTP</acronym>
  199. レスポンスコードを設定したり取得したりするメソッドとして
  200. <methodname>setHttpResponseCode()</methodname> と
  201. <methodname>getHttpResponseCode()</methodname> が用意されています。
  202. </para>
  203. <sect3 id="zend.controller.response.headers.setcookie">
  204. <title>クッキーのヘッダーを設定</title>
  205. <!-- TODO : to be translated -->
  206. <para>
  207. You can inject <acronym>HTTP</acronym> Set-Cookie headers into the response object
  208. of an action controller by using the provided header class
  209. <classname>Zend_Http_Header_SetCookie</classname>
  210. </para>
  211. <sect4 id="zend.controller.response.headers.setcookie.constructor">
  212. <title>コンストラクタの引数</title>
  213. <para>
  214. The following table lists all constructor arguments of
  215. <classname>Zend_Http_Header_SetCookie</classname>
  216. in the order they are accepted. All arguments are optional,
  217. but name and value must be supplied via their setters if not
  218. passed in via the constructor or the resulting Set-Cookie header
  219. be invalid.
  220. </para>
  221. <itemizedlist>
  222. <listitem>
  223. <para>
  224. <varname>$name</varname>: クッキーの名前
  225. </para>
  226. </listitem>
  227. <listitem>
  228. <para>
  229. <varname>$value</varname>: クッキーの値
  230. </para>
  231. </listitem>
  232. <listitem>
  233. <para>
  234. <varname>$expires</varname>: The time the cookie expires
  235. </para>
  236. </listitem>
  237. <listitem>
  238. <para>
  239. <varname>$path</varname>: The path on the server in which
  240. the cookie will be available
  241. </para>
  242. </listitem>
  243. <listitem>
  244. <para>
  245. <varname>$domain</varname>: The domain to restrict cookie to
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <varname>$secure</varname>: boolean indicating whether cookie
  251. should be sent over an unencrypted connection (false) or via
  252. <acronym>HTTPS</acronym> only (true)
  253. </para>
  254. </listitem>
  255. <listitem>
  256. <para>
  257. <varname>$httpOnly</varname>: boolean indicating whether cookie
  258. should be transmitted only via the <acronym>HTTP</acronym> protocol
  259. </para>
  260. </listitem>
  261. <listitem>
  262. <para>
  263. <varname>$maxAge</varname>: The maximum age of the cookie in seconds
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <varname>$version</varname>: The cookie specification version
  269. </para>
  270. </listitem>
  271. </itemizedlist>
  272. <example>
  273. <title>Populate Zend_Http_Header_SetCookie via constructor and add to response</title>
  274. <programlisting language="php"><![CDATA[
  275. $this->getResponse()->setRawHeader(new Zend_Http_Header_SetCookie(
  276. 'foo', 'bar', NULL, '/', 'example.com', false, true
  277. ));
  278. ]]></programlisting>
  279. </example>
  280. <example>
  281. <title>Populate Zend_Http_Header_SetCookie via setters and add to response</title>
  282. <programlisting language="php"><![CDATA[
  283. $cookie = new Zend_Http_Header_SetCookie();
  284. $cookie->setName('foo')
  285. ->setValue('bar')
  286. ->setDomain('example.com')
  287. ->setPath('/')
  288. ->setHttponly(true);
  289. $this->getResponse()->setRawHeader($cookie);
  290. ]]></programlisting>
  291. </example>
  292. </sect4>
  293. </sect3>
  294. </sect2>
  295. <sect2 id="zend.controller.response.namedsegments">
  296. <title>名前つきセグメント</title>
  297. <para>
  298. レスポンスオブジェクトでは「名前つきセグメント」をサポートしています。
  299. これにより、本文部だけを別のセグメントに切り分けて、
  300. 指定した順序で出力したりといったことができるようになります。
  301. 内部的にはコンテンツは配列として保存され、
  302. さまざまなメソッドを使用してその配列にアクセスできるようになります。
  303. </para>
  304. <para>
  305. 例として、<methodname>preDispatch()</methodname> フックメソッドで
  306. レスポンスオブジェクトにヘッダを追加し、
  307. アクションコントローラで本文を追加して、
  308. 最後に <methodname>postDispatch()</methodname> フックメソッドでフッタを追加することを考えてみましょう。
  309. </para>
  310. <programlisting language="php"><![CDATA[
  311. // このプラグインクラスがフロントコントローラで登録済みであると仮定します
  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. // アクションコントローラの例
  330. class MyController extends Zend_Controller_Action
  331. {
  332. public function fooAction()
  333. {
  334. $this->render();
  335. }
  336. }
  337. ]]></programlisting>
  338. <para>
  339. 上の例で <filename>/my/foo</filename> をコールすると、
  340. レスポンスオブジェクトに最終的に格納されるコンテンツは次のようになります。
  341. </para>
  342. <programlisting language="php"><![CDATA[
  343. array(
  344. 'header' => ..., // ヘッダの内容
  345. 'default' => ..., // MyController::fooAction() が作成した本文
  346. 'footer' => ... // フッタの内容
  347. );
  348. ]]></programlisting>
  349. <para>
  350. これをレンダリングすると、配列に要素が追加された順に表示されます。
  351. </para>
  352. <para>
  353. 名前つきセグメントを操作するメソッドには、以下のようなものがあります。
  354. </para>
  355. <itemizedlist>
  356. <listitem>
  357. <para>
  358. <methodname>setBody()</methodname>
  359. の二番目のパラメータである <varname>$name</varname>
  360. に、セグメント名を渡すことができます。
  361. セグメント名を指定すると、指定したセグメントの内容を上書きします
  362. (存在しない場合は新たに作成し、body 配列に追加します)。
  363. <methodname>setBody()</methodname> にセグメント名を指定しなかった場合は、
  364. 配列全体を初期化します。
  365. </para>
  366. </listitem>
  367. <listitem>
  368. <para>
  369. <!-- TODO : to be translated -->
  370. <methodname>appendBody()</methodname> also allows you to pass
  371. a second value, <varname>$name</varname>, indicating a named segment.
  372. If you provide a segment name it will append the supplied content
  373. to the existing content in the named segment, or create the segment
  374. if it does not exist (appending to the body array by
  375. default). <methodname>appendBody()</methodname>
  376. でセグメント名を省略した場合は、it will append the supplied
  377. content to the named segment 'default', creating it if it does not
  378. already exist.
  379. </para>
  380. </listitem>
  381. <listitem>
  382. <para>
  383. <methodname>prepend($name, $content)</methodname> は、
  384. <varname>$name</varname> という名前のセグメントを作成して、
  385. それを配列の先頭に追加します。同じ名前のセグメントが存在する場合は、
  386. まずそれを削除してから追加します(つまり、既存のものを上書きします)。
  387. </para>
  388. </listitem>
  389. <listitem>
  390. <para>
  391. <methodname>append($name, $content)</methodname> は、
  392. <varname>$name</varname> という名前のセグメントを作成して、
  393. それを配列の最後に追加します。同じ名前のセグメントが存在する場合は、
  394. まずそれを削除してから追加します(つまり、既存のものを上書きします)。
  395. </para>
  396. </listitem>
  397. <listitem>
  398. <para>
  399. <methodname>insert($name, $content, $parent = null, $before =
  400. false)</methodname> は、<varname>$name</varname> という名前のセグメントを作成します。
  401. <varname>$parent</varname> セグメントを指定すると、
  402. 新しいセグメントはそのセグメントの前か後ろ
  403. (<varname>$before</varname> の値で決まります)
  404. に配置されます。同じ名前のセグメントが存在する場合は、
  405. まずそれを削除してから追加します(つまり、既存のものを上書きします)。
  406. </para>
  407. </listitem>
  408. <listitem>
  409. <para>
  410. <methodname>clearBody($name = null)</methodname>
  411. に <varname>$name</varname> を指定すると、その名前のセグメントを消去します
  412. (省略した場合は、配列全体を消去します)。
  413. </para>
  414. </listitem>
  415. <listitem>
  416. <para>
  417. <methodname>getBody($spec = false)</methodname> で
  418. <varname>$spec</varname> にセグメント名を指定すると、そのセグメントを取得できます。
  419. <varname>$spec</varname> に <constant>FALSE</constant> を指定すると、
  420. すべてのセグメントの内容を順番に連結した結果を文字列で返します。
  421. <varname>$spec</varname> に <constant>TRUE</constant> を指定すると、本文の配列を返します。
  422. </para>
  423. </listitem>
  424. </itemizedlist>
  425. </sect2>
  426. <sect2 id="zend.controller.response.exceptions">
  427. <title>レスポンスオブジェクト内での例外の検査</title>
  428. <para>
  429. 先ほど説明したように、デフォルトでは
  430. ディスパッチ中に発生した例外はレスポンスオブジェクトに登録されます。
  431. 例外はスタックに登録されるので、発生した例外はすべて保持することができます。
  432. アプリケーションの例外、ディスパッチ処理の例外、プラグインの例外などなど……。
  433. 特定の例外の内容を調べたり例外をログに記録したりしたい場合は、
  434. レスポンスオブジェクトの例外用 <acronym>API</acronym> を使用します。
  435. </para>
  436. <itemizedlist>
  437. <listitem>
  438. <para>
  439. <methodname>setException(Exception $e)</methodname>
  440. は、例外を登録します。
  441. </para>
  442. </listitem>
  443. <listitem>
  444. <para>
  445. <methodname>isException()</methodname>
  446. は、例外が登録されているかどうかを調べます。
  447. </para>
  448. </listitem>
  449. <listitem>
  450. <para>
  451. <methodname>getException()</methodname>
  452. は、例外スタック全体を返します。
  453. </para>
  454. </listitem>
  455. <listitem>
  456. <para>
  457. <methodname>hasExceptionOfType($type)</methodname>
  458. は、特定のクラスの例外がスタックに登録されているかどうかを調べます。
  459. </para>
  460. </listitem>
  461. <listitem>
  462. <para>
  463. <methodname>hasExceptionOfMessage($message)</methodname>
  464. は、指定したメッセージを含む例外が
  465. スタックに登録されているかどうかを調べます。
  466. </para>
  467. </listitem>
  468. <listitem>
  469. <para>
  470. <methodname>hasExceptionOfCode($code)</methodname>
  471. は、指定したコードを含む例外が
  472. スタックに登録されているかどうかを調べます。
  473. </para>
  474. </listitem>
  475. <listitem>
  476. <para>
  477. <methodname>getExceptionByType($type)</methodname>
  478. は、指定したクラスの例外をスタックからすべて取り出します。
  479. そのクラスの例外が見つからなかった場合は <constant>FALSE</constant> を返し、
  480. 見つかった場合は例外の配列を返します。
  481. </para>
  482. </listitem>
  483. <listitem>
  484. <para>
  485. <methodname>getExceptionByMessage($message)</methodname>
  486. は、指定したメッセージを含む例外をスタックからすべて取り出します。
  487. そのクラスの例外が見つからなかった場合は <constant>FALSE</constant> を返し、
  488. 見つかった場合は例外の配列を返します。
  489. </para>
  490. </listitem>
  491. <listitem>
  492. <para>
  493. <methodname>getExceptionByCode($code)</methodname>
  494. は、指定したコードを含む例外をスタックからすべて取り出します。
  495. そのクラスの例外が見つからなかった場合は <constant>FALSE</constant> を返し、
  496. 見つかった場合は例外の配列を返します。
  497. </para>
  498. </listitem>
  499. <listitem>
  500. <para>
  501. <methodname>renderExceptions($flag)</methodname>
  502. は、例外が発生したかどうかを表すフラグを設定します。
  503. </para>
  504. </listitem>
  505. </itemizedlist>
  506. </sect2>
  507. <sect2 id="zend.controller.response.subclassing">
  508. <title>レスポンスオブジェクトのサブクラスの作成</title>
  509. <para>
  510. レスポンスオブジェクトの役割は、
  511. さまざまなアクションやプラグインからヘッダやコンテンツを収集し、
  512. それをクライアントに返すことです。
  513. さらに、処理中に発生したエラーの内容も収集します。
  514. これはそのまま返すこともありますし、あるいはユーザから見えないように隠すこともあります。
  515. </para>
  516. <para>
  517. レスポンスクラスの基底クラスは
  518. <classname>Zend_Controller_Response_Abstract</classname>
  519. です。レスポンスクラスを作成する際には、
  520. このクラスあるいはその派生クラスのいずれかを継承しなければなりません。
  521. このクラスが提供するメソッドについては、先ほど説明しました。
  522. </para>
  523. <para>
  524. レスポンスオブジェクトのサブクラスを作成する理由としては、
  525. リクエストされた環境に応じて出力内容を変えたり
  526. (たとえば <acronym>CLI</acronym> や
  527. <acronym>PHP</acronym>-<acronym>GTK</acronym> の場合はヘッダを送信しないなど)
  528. 名前つきセグメントに保存された内容の最終結果を返す機能を追加したりといったことが考えられます。
  529. </para>
  530. </sect2>
  531. </sect1>
  532. <!--
  533. vim:se ts=4 sw=4 et:
  534. -->