From 65e91e8a2ecbad19a236253048f1e505554dade4 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sat, 4 Mar 2017 08:58:37 +0900 Subject: [PATCH 1/2] Fix whitespace. --- en/controllers/request-response.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/controllers/request-response.rst b/en/controllers/request-response.rst index 86d4f4b525..27e837b037 100644 --- a/en/controllers/request-response.rst +++ b/en/controllers/request-response.rst @@ -591,7 +591,7 @@ To set a string as the response body, do the following:: ->withStringBody(json_encode(['Foo' => 'bar'])); .. versionadded:: 3.4.3 -   ``withStringBody()`` was added in 3.4.3 + ``withStringBody()`` was added in 3.4.3 .. php:method:: withBody($body) From 346a0ba86ba0e9ac262b08f6678381b14fb2ece3 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sat, 4 Mar 2017 09:01:03 +0900 Subject: [PATCH 2/2] [ja] follows #4771 --- ja/controllers/request-response.rst | 102 +++++++++++++++++----------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/ja/controllers/request-response.rst b/ja/controllers/request-response.rst index 6dc68abfdc..ec2955fe54 100644 --- a/ja/controllers/request-response.rst +++ b/ja/controllers/request-response.rst @@ -508,16 +508,72 @@ download return $response; } -ストリーミングリソース ----------------------- +ヘッダの設定 +------------ + +.. php:method:: withHeader($header, $value) + +ヘッダーの設定は :php:meth:`Cake\\Http\\Response::withHeader()` メソッドで行われます。 +すべての PSR-7 インターフェイスのメソッドと同様に、このメソッドは新しいヘッダーを含む +*新しい* インスタンスを返します。 :: + + // 一つのヘッダーを追加/置換 + $response = $response->withHeader('X-Extra', 'My header'); + + // 一度に複数ヘッダーを設定 + $response = $response->withHeader('X-Extra', 'My header') + ->withHeader('Location', 'http://example.com'); + + // 既存のヘッダーに値を追加 + $response = $response->withAddedHeader('Set-Cookie', 'remember_me=1'); + + // 3.4.0 より前 - 一つのヘッダーを設定 + $this->response->header('Location', 'http://example.com'); + +セットされた際、ヘッダは送られません。これらのヘッダは、 ``Cake\Http\Server`` によって +レスポンスが実際に送られるまで保持されます。 + +便利なメソッド :php:meth:`Cake\\Http\\Response::withLocation()` を使うと +直接リダイレクトヘッダの設定や取得ができます。 + +ボディの設定 +------------ + +.. php:method:: withStringBody($string) + +レスポンスボディとして文字列を設定するには、次のようにします。 :: + + // ボディの中に文字列をセット + $response = $response->withStringBody('My Body'); + + // json レスポンスにしたい場合 + $response = $response->withType('application/json') + ->withStringBody(json_encode(['Foo' => 'bar'])); + +.. versionadded:: 3.4.3 + ``withStringBody()`` は 3.4.3 で追加されました。 + +.. php:method:: withBody($body) -Diactoros の Stream を使用してファイルのレスポンスをストリーム化できます。 :: +``withBody()`` を使って、 :php:class:`Zend\\Diactoros\\MessageTrait` によって提供される +レスポンスボディを設定するには、 :: + + $response = $response->withBody($stream); + + // 3.4.0 より前でボディを設定 + $this->response->body('My Body'); + +``$stream`` が :php:class:`Psr\\Http\\Message\\StreamInterface` +オブジェクトであることを確認してください。新しいストリームを作成する方法は、以下をご覧ください。 + +:php:class:`Zend\\Diactoros\\Stream` ストリームを使用して、 +ファイルからレスポンスをストリーム化することもできます。 :: // ファイルからのストリーム化 use Zend\Diactoros\Stream; $stream = new Stream('/path/to/file', 'rb'); - $response = $response->withStream($stream); + $response = $response->withBody($stream); また、 ``CallbackStream`` を使用してコールバックをストリーム化できます。 クライアントへストリーム化する必要のある画像、CSV ファイル もしくは PDF @@ -533,7 +589,7 @@ Diactoros の Stream を使用してファイルのレスポンスをストリ $stream = new CallbackStream(function () use ($img) { imagepng($img); }); - $response = $response->withStream($stream); + $response = $response->withBody($stream); // 3.4.0 より前では、次のようにストリーミングレスポンスを作成することができます。 $file = fopen('/some/file.png', 'r'); @@ -543,42 +599,6 @@ Diactoros の Stream を使用してファイルのレスポンスをストリ fclose($file); }); -コールバックはまた、文字列としてボディを返すことができます。 :: - - $path = '/some/file.png'; - $this->response->body(function () use ($path) { - return file_get_contents($path); - }); - - -ヘッダの設定 ------------- - -.. php:method:: withHeader($header, $value) - -ヘッダーの設定は :php:meth:`Cake\\Http\\Response::withHeader()` メソッドで行われます。 -このメソッドは少し違ったパラメータ設定と一緒に呼ばれます。すべての PSR-7 インターフェイスの -メソッドと同様に、このメソッドは新しいヘッダーを含む *新しい* インスタンスを返します。 :: - - // 一つのヘッダーを追加/置換 - $response = $response->withHeader('X-Extra', 'My header'); - - // 一度に複数ヘッダーを設定 - $response = $response->withHeader('X-Extra', 'My header') - ->withHeader('Location', 'http://example.com'); - - // 既存のヘッダーに値を追加 - $response = $response->withAddedHeader('Set-Cookie', 'remember_me=1'); - - // 3.4.0 より前 - 一つのヘッダーを設定 - $this->response->header('Location', 'http://example.com'); - -セットされた際、ヘッダは送られません。これらのヘッダは、 ``Cake\Http\Server`` によって -レスポンスが実際に送られるまで保持されます。 - -便利なメソッド :php:meth:`Cake\\Http\\Response::withLocation()` を使うと -直接リダイレクトヘッダの設定や取得ができます。 - 文字コードの設定 ----------------