Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion en/controllers/request-response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
102 changes: 61 additions & 41 deletions ja/controllers/request-response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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()` を使うと
直接リダイレクトヘッダの設定や取得ができます。

文字コードの設定
----------------

Expand Down