| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <!-- EN-Revision: 24997 -->
- <sect1 id="zend.controller.response">
- <title>レスポンスオブジェクト</title>
- <sect2 id="zend.controller.response.usage">
- <title>使用法</title>
- <para>
- レスポンスオブジェクトは、
- <link linkend="zend.controller.request">リクエストオブジェクト</link>
- と対になるものです。
- その目的は、コンテンツやヘッダを収集し、それを返すことです。
- さらに、フロントコントローラで捕捉した例外はすべてレスポンスオブジェクトに渡されます。
- これにより、例外の処理がやりやすくなります。
- この挙動を変更するには
- <methodname>Zend_Controller_Front::throwExceptions(true)</methodname>
- と設定します。
- </para>
- <programlisting language="php"><![CDATA[
- $front->throwExceptions(true);
- ]]></programlisting>
- <para>
- ヘッダを含むレスポンス出力を送信するには、
- <methodname>sendResponse()</methodname> を使用します。
- </para>
- <programlisting language="php"><![CDATA[
- $response->sendResponse();
- ]]></programlisting>
- <note>
- <para>
- デフォルトでは、リクエストのディスパッチに終了した時点でフロントコントローラが
- <methodname>sendResponse()</methodname> をコールします。通常はこれをコールする必要はありません。
- しかし、テスト中などにレスポンスの内容を操作したい場合は、
- <property>returnResponse</property> フラグを
- <methodname>Zend_Controller_Front::returnResponse(true)</methodname>
- と設定することでこの振る舞いを変更できます。
- </para>
- <programlisting language="php"><![CDATA[
- $front->returnResponse(true);
- $response = $front->dispatch();
- // 何かの処理、たとえばログの記録などを行ってから
- // 結果を出力します
- $response->sendResponse();
- ]]></programlisting>
- </note>
- <para>
- 開発者は、アクションコントローラの中でレスポンスオブジェクトを使用しなければなりません。
- 出力を直接レンダリングしたり直接ヘッダを送信したりするのではなく、
- それらをレスポンスオブジェクトに格納するようにします。
- </para>
- <programlisting language="php"><![CDATA[
- // アクションコントローラのアクション内で、
- // ヘッダを設定します
- $this->getResponse()
- ->setHeader('Content-Type', 'text/html')
- ->appendBody($content);
- ]]></programlisting>
- <para>
- こうすることで、すべてのヘッダを一度に送信し、
- その後でコンテンツを表示することができます。
- </para>
- <note>
- <para>
- アクションコントローラで <link
- linkend="zend.controller.action.viewintegration">ビューの統合
- </link> を使用する場合は、
- レンダリングされたビュースクリプトの内容をレスポンスオブジェクトに設定する必要はありません。
- <methodname>Zend_Controller_Action::render()</methodname> がデフォルトでこれを行います。
- </para>
- </note>
- <para>
- アプリケーションで例外が発生したかどうかを調べるには、
- レスポンスオブジェクトの <methodname>isException()</methodname>
- フラグを調べます。例外を取得するには <methodname>getException()</methodname>
- を使用します。さらに、独自のレスポンスオブジェクトを作成して、
- エラーページへのリダイレクトや例外メッセージのログ出力、
- 例外をわかりやすく表示する (開発用) などを行うことができます。
- </para>
- <para>
- レスポンスオブジェクトは、フロントコントローラの
- <methodname>dispatch()</methodname> から受け取ることになります。あるいは、
- 出力のレンダリングを行わない状態のレスポンスオブジェクトを
- フロントコントローラから受け取ることもできます。
- </para>
- <programlisting language="php"><![CDATA[
- // dispatch の後に取得します
- $front->dispatch();
- $response = $front->getResponse();
- if ($response->isException()) {
- // ログへの記録、メール送信など...
- }
- // あるいは、フロントコントローラの dispatch() の返り値を使用します
- $front->returnResponse(true);
- $response = $front->dispatch();
- // 何かの処理...
- // 最後に結果を表示します
- $response->sendResponse();
- ]]></programlisting>
- <para>
- デフォルトでは、例外メッセージは表示されません。
- この挙動をオーバーライドするには <methodname>renderExceptions()</methodname>
- メソッドを使用するか、あるいは上で示したようにフロントコントローラで
- <methodname>throwExceptions()</methodname> を有効にします。
- </para>
- <programlisting language="php"><![CDATA[
- $response->renderExceptions(true);
- $front->dispatch($request, $response);
- // あるいは
- $front->returnResponse(true);
- $response = $front->dispatch();
- $response->renderExceptions();
- $response->sendResponse();
- // あるいは
- $front->throwExceptions(true);
- $front->dispatch();
- ]]></programlisting>
- </sect2>
- <sect2 id="zend.controller.response.headers">
- <title>ヘッダの操作</title>
- <para>
- 先ほど説明したように、レスポンスオブジェクトの役割のひとつは
- <acronym>HTTP</acronym> レスポンスヘッダを発行することです。
- このために、さまざまなメソッドが用意されています。
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <methodname>canSendHeaders()</methodname> を使用して、
- ヘッダがすでに送信されているかどうかを調べます。
- オプションのフラグで、ヘッダが送信済みの場合に例外をスローするかどうかを指定します。
- この設定は、プロパティ <property>headersSentThrowsException</property>
- を <constant>FALSE</constant> にすることで上書きできます。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setHeader($name, $value, $replace = false)</methodname>
- を使用して、個々のヘッダを設定します。デフォルトでは、
- 同名のヘッダがすでに存在した場合に既存のヘッダを置換することはありません。
- しかし、<varname>$replace</varname> を <constant>TRUE</constant> に設定すると、
- 既存のヘッダを上書きするようになります。
- </para>
- <para>
- ヘッダを設定する前に、このメソッドは
- <methodname>canSendHeaders()</methodname> を使用して
- ヘッダが現時点で送信済みでないかどうか、例外をスローするかどうかを調べます。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setRedirect($url, $code = 302)</methodname> は、
- リダイレクト用の <acronym>HTTP</acronym> Location ヘッダを設定します。
- <acronym>HTTP</acronym> ステータスコードを指定した場合は、そのコードを使用します。
- </para>
- <para>
- 内部的には、このメソッドは
- <varname>$replace</varname> フラグをオンにして
- <methodname>setHeader()</methodname> をコールしています。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getHeaders()</methodname> は、すべてのヘッダを配列で返します。
- 個々の配列の要素は、'name' および 'value'
- のふたつのキーを持つ配列となります。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>clearHeaders()</methodname> は登録済みのヘッダをすべて削除します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setRawHeader()</methodname>
- を使用して、キー/値 の組になっていないヘッダを設定します。
- たとえば <acronym>HTTP</acronym> status ヘッダなどがこれにあたります。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getRawHeaders()</methodname> は、登録済みの生のヘッダを返します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>clearRawHeaders()</methodname> は、登録済みの生のヘッダを消去します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>clearAllHeaders()</methodname> は、キー/値 のペアである通常のヘッダと
- 生のヘッダの両方を消去します。
- </para>
- </listitem>
- </itemizedlist>
- <para>
- これらのメソッドのほかに、現在のリクエストの <acronym>HTTP</acronym>
- レスポンスコードを設定したり取得したりするメソッドとして
- <methodname>setHttpResponseCode()</methodname> と
- <methodname>getHttpResponseCode()</methodname> が用意されています。
- </para>
- <sect3 id="zend.controller.response.headers.setcookie">
- <title>クッキーのヘッダーを設定</title>
- <!-- TODO : to be translated -->
- <para>
- You can inject <acronym>HTTP</acronym> Set-Cookie headers into the response object
- of an action controller by using the provided header class
- <classname>Zend_Http_Header_SetCookie</classname>
- </para>
- <sect4 id="zend.controller.response.headers.setcookie.constructor">
- <title>コンストラクタの引数</title>
- <para>
- The following table lists all constructor arguments of
- <classname>Zend_Http_Header_SetCookie</classname>
- in the order they are accepted. All arguments are optional,
- but name and value must be supplied via their setters if not
- passed in via the constructor or the resulting Set-Cookie header
- be invalid.
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <varname>$name</varname>: クッキーの名前
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$value</varname>: クッキーの値
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$expires</varname>: The time the cookie expires
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$path</varname>: The path on the server in which
- the cookie will be available
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$domain</varname>: The domain to restrict cookie to
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$secure</varname>: boolean indicating whether cookie
- should be sent over an unencrypted connection (false) or via
- <acronym>HTTPS</acronym> only (true)
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$httpOnly</varname>: boolean indicating whether cookie
- should be transmitted only via the <acronym>HTTP</acronym> protocol
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$maxAge</varname>: The maximum age of the cookie in seconds
- </para>
- </listitem>
- <listitem>
- <para>
- <varname>$version</varname>: The cookie specification version
- </para>
- </listitem>
- </itemizedlist>
- <example>
- <title>Populate Zend_Http_Header_SetCookie via constructor and add to response</title>
- <programlisting language="php"><![CDATA[
- $this->getResponse()->setRawHeader(new Zend_Http_Header_SetCookie(
- 'foo', 'bar', NULL, '/', 'example.com', false, true
- ));
- ]]></programlisting>
- </example>
- <example>
- <title>Populate Zend_Http_Header_SetCookie via setters and add to response</title>
- <programlisting language="php"><![CDATA[
- $cookie = new Zend_Http_Header_SetCookie();
- $cookie->setName('foo')
- ->setValue('bar')
- ->setDomain('example.com')
- ->setPath('/')
- ->setHttponly(true);
- $this->getResponse()->setRawHeader($cookie);
- ]]></programlisting>
- </example>
- </sect4>
- </sect3>
- </sect2>
- <sect2 id="zend.controller.response.namedsegments">
- <title>名前つきセグメント</title>
- <para>
- レスポンスオブジェクトでは「名前つきセグメント」をサポートしています。
- これにより、本文部だけを別のセグメントに切り分けて、
- 指定した順序で出力したりといったことができるようになります。
- 内部的にはコンテンツは配列として保存され、
- さまざまなメソッドを使用してその配列にアクセスできるようになります。
- </para>
- <para>
- 例として、<methodname>preDispatch()</methodname> フックメソッドで
- レスポンスオブジェクトにヘッダを追加し、
- アクションコントローラで本文を追加して、
- 最後に <methodname>postDispatch()</methodname> フックメソッドでフッタを追加することを考えてみましょう。
- </para>
- <programlisting language="php"><![CDATA[
- // このプラグインクラスがフロントコントローラで登録済みであると仮定します
- class MyPlugin extends Zend_Controller_Plugin_Abstract
- {
- public function preDispatch(Zend_Controller_Request_Abstract $request)
- {
- $response = $this->getResponse();
- $view = new Zend_View();
- $view->setBasePath('../views/scripts');
- $response->prepend('header', $view->render('header.phtml'));
- }
- public function postDispatch(Zend_Controller_Request_Abstract $request)
- {
- $response = $this->getResponse();
- $view = new Zend_View();
- $view->setBasePath('../views/scripts');
- $response->append('footer', $view->render('footer.phtml'));
- }
- }
- // アクションコントローラの例
- class MyController extends Zend_Controller_Action
- {
- public function fooAction()
- {
- $this->render();
- }
- }
- ]]></programlisting>
- <para>
- 上の例で <filename>/my/foo</filename> をコールすると、
- レスポンスオブジェクトに最終的に格納されるコンテンツは次のようになります。
- </para>
- <programlisting language="php"><![CDATA[
- array(
- 'header' => ..., // ヘッダの内容
- 'default' => ..., // MyController::fooAction() が作成した本文
- 'footer' => ... // フッタの内容
- );
- ]]></programlisting>
- <para>
- これをレンダリングすると、配列に要素が追加された順に表示されます。
- </para>
- <para>
- 名前つきセグメントを操作するメソッドには、以下のようなものがあります。
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <methodname>setBody()</methodname>
- の二番目のパラメータである <varname>$name</varname>
- に、セグメント名を渡すことができます。
- セグメント名を指定すると、指定したセグメントの内容を上書きします
- (存在しない場合は新たに作成し、body 配列に追加します)。
- <methodname>setBody()</methodname> にセグメント名を指定しなかった場合は、
- 配列全体を初期化します。
- </para>
- </listitem>
- <listitem>
- <para>
- <!-- TODO : to be translated -->
- <methodname>appendBody()</methodname> also allows you to pass
- a second value, <varname>$name</varname>, indicating a named segment.
- If you provide a segment name it will append the supplied content
- to the existing content in the named segment, or create the segment
- if it does not exist (appending to the body array by
- default). <methodname>appendBody()</methodname>
- でセグメント名を省略した場合は、it will append the supplied
- content to the named segment 'default', creating it if it does not
- already exist.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>prepend($name, $content)</methodname> は、
- <varname>$name</varname> という名前のセグメントを作成して、
- それを配列の先頭に追加します。同じ名前のセグメントが存在する場合は、
- まずそれを削除してから追加します(つまり、既存のものを上書きします)。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>append($name, $content)</methodname> は、
- <varname>$name</varname> という名前のセグメントを作成して、
- それを配列の最後に追加します。同じ名前のセグメントが存在する場合は、
- まずそれを削除してから追加します(つまり、既存のものを上書きします)。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>insert($name, $content, $parent = null, $before =
- false)</methodname> は、<varname>$name</varname> という名前のセグメントを作成します。
- <varname>$parent</varname> セグメントを指定すると、
- 新しいセグメントはそのセグメントの前か後ろ
- (<varname>$before</varname> の値で決まります)
- に配置されます。同じ名前のセグメントが存在する場合は、
- まずそれを削除してから追加します(つまり、既存のものを上書きします)。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>clearBody($name = null)</methodname>
- に <varname>$name</varname> を指定すると、その名前のセグメントを消去します
- (省略した場合は、配列全体を消去します)。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getBody($spec = false)</methodname> で
- <varname>$spec</varname> にセグメント名を指定すると、そのセグメントを取得できます。
- <varname>$spec</varname> に <constant>FALSE</constant> を指定すると、
- すべてのセグメントの内容を順番に連結した結果を文字列で返します。
- <varname>$spec</varname> に <constant>TRUE</constant> を指定すると、本文の配列を返します。
- </para>
- </listitem>
- </itemizedlist>
- </sect2>
- <sect2 id="zend.controller.response.exceptions">
- <title>レスポンスオブジェクト内での例外の検査</title>
- <para>
- 先ほど説明したように、デフォルトでは
- ディスパッチ中に発生した例外はレスポンスオブジェクトに登録されます。
- 例外はスタックに登録されるので、発生した例外はすべて保持することができます。
- アプリケーションの例外、ディスパッチ処理の例外、プラグインの例外などなど……。
- 特定の例外の内容を調べたり例外をログに記録したりしたい場合は、
- レスポンスオブジェクトの例外用 <acronym>API</acronym> を使用します。
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <methodname>setException(Exception $e)</methodname>
- は、例外を登録します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>isException()</methodname>
- は、例外が登録されているかどうかを調べます。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getException()</methodname>
- は、例外スタック全体を返します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>hasExceptionOfType($type)</methodname>
- は、特定のクラスの例外がスタックに登録されているかどうかを調べます。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>hasExceptionOfMessage($message)</methodname>
- は、指定したメッセージを含む例外が
- スタックに登録されているかどうかを調べます。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>hasExceptionOfCode($code)</methodname>
- は、指定したコードを含む例外が
- スタックに登録されているかどうかを調べます。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getExceptionByType($type)</methodname>
- は、指定したクラスの例外をスタックからすべて取り出します。
- そのクラスの例外が見つからなかった場合は <constant>FALSE</constant> を返し、
- 見つかった場合は例外の配列を返します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getExceptionByMessage($message)</methodname>
- は、指定したメッセージを含む例外をスタックからすべて取り出します。
- そのクラスの例外が見つからなかった場合は <constant>FALSE</constant> を返し、
- 見つかった場合は例外の配列を返します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getExceptionByCode($code)</methodname>
- は、指定したコードを含む例外をスタックからすべて取り出します。
- そのクラスの例外が見つからなかった場合は <constant>FALSE</constant> を返し、
- 見つかった場合は例外の配列を返します。
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>renderExceptions($flag)</methodname>
- は、例外が発生したかどうかを表すフラグを設定します。
- </para>
- </listitem>
- </itemizedlist>
- </sect2>
- <sect2 id="zend.controller.response.subclassing">
- <title>レスポンスオブジェクトのサブクラスの作成</title>
- <para>
- レスポンスオブジェクトの役割は、
- さまざまなアクションやプラグインからヘッダやコンテンツを収集し、
- それをクライアントに返すことです。
- さらに、処理中に発生したエラーの内容も収集します。
- これはそのまま返すこともありますし、あるいはユーザから見えないように隠すこともあります。
- </para>
- <para>
- レスポンスクラスの基底クラスは
- <classname>Zend_Controller_Response_Abstract</classname>
- です。レスポンスクラスを作成する際には、
- このクラスあるいはその派生クラスのいずれかを継承しなければなりません。
- このクラスが提供するメソッドについては、先ほど説明しました。
- </para>
- <para>
- レスポンスオブジェクトのサブクラスを作成する理由としては、
- リクエストされた環境に応じて出力内容を変えたり
- (たとえば <acronym>CLI</acronym> や
- <acronym>PHP</acronym>-<acronym>GTK</acronym> の場合はヘッダを送信しないなど)
- 名前つきセグメントに保存された内容の最終結果を返す機能を追加したりといったことが考えられます。
- </para>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|