From baed142937c1057887d7b690b8f513fa0e562011 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Mon, 27 Feb 2017 09:48:08 +0900 Subject: [PATCH] [ja] follows #4746 --- ja/controllers/request-response.rst | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ja/controllers/request-response.rst b/ja/controllers/request-response.rst index bf655a77b0..6dc68abfdc 100644 --- a/ja/controllers/request-response.rst +++ b/ja/controllers/request-response.rst @@ -511,8 +511,31 @@ download ストリーミングリソース ---------------------- -リソースストリームをレスポンスに変換するために ``body()`` でコールバックを使用することができます。 :: +Diactoros の Stream を使用してファイルのレスポンスをストリーム化できます。 :: + // ファイルからのストリーム化 + use Zend\Diactoros\Stream; + + $stream = new Stream('/path/to/file', 'rb'); + $response = $response->withStream($stream); + +また、 ``CallbackStream`` を使用してコールバックをストリーム化できます。 +クライアントへストリーム化する必要のある画像、CSV ファイル もしくは PDF +のようなリソースがある場合に便利です。 :: + + // コールバックからのストリーム化 + use Cake\Http\CallbackStream; + + // 画像の作成 + $img = imagecreate(100, 100); + // ... + + $stream = new CallbackStream(function () use ($img) { + imagepng($img); + }); + $response = $response->withStream($stream); + + // 3.4.0 より前では、次のようにストリーミングレスポンスを作成することができます。 $file = fopen('/some/file.png', 'r'); $this->response->body(function () use ($file) { rewind($file);