You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: responses.md
+47-9Lines changed: 47 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@
14
14
-[JSON Responses](#json-responses)
15
15
-[File Downloads](#file-downloads)
16
16
-[File Responses](#file-responses)
17
+
-[Streamed Responses](#streamed-responses)
17
18
-[Response Macros](#response-macros)
18
19
19
20
<aname="creating-responses"></a>
@@ -287,6 +288,52 @@ The `download` method may be used to generate a response that forces the user's
287
288
> [!WARNING]
288
289
> Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII filename.
289
290
291
+
<aname="file-responses"></a>
292
+
### File Responses
293
+
294
+
The `file` method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download. This method accepts the absolute path to the file as its first argument and an array of headers as its second argument:
295
+
296
+
return response()->file($pathToFile);
297
+
298
+
return response()->file($pathToFile, $headers);
299
+
300
+
<aname="streamed-responses"></a>
301
+
### Streamed Responses
302
+
303
+
By streaming data to the client as it is generated, you can significantly reduce memory usage and improve performance, especially for very large responses. Streamed responses allow the client to begin processing data before the server has finished sending it:
304
+
305
+
function streamedContent(): Generator {
306
+
yield 'Hello, ';
307
+
yield 'World!';
308
+
}
309
+
310
+
Route::get('/stream', function () {
311
+
return response()->stream(function (): void {
312
+
foreach (streamedContent() as $chunk) {
313
+
echo $chunk;
314
+
ob_flush();
315
+
flush();
316
+
sleep(2); // Simulate delay between chunks...
317
+
}
318
+
}, 200, ['X-Accel-Buffering' => 'no']);
319
+
});
320
+
321
+
> [!NOTE]
322
+
> Internally, Laravel utilizes PHP's output buffering functionality. As you can see in the example above, you should use the `ob_flush` and `flush` functions to push buffered content to the client.
323
+
324
+
<aname="streamed-json-responses"></a>
325
+
#### Streamed JSON Responses
326
+
327
+
If you need to stream JSON data incrementally, you may utilize the `streamJson` method. This method is especially useful for large datasets that need to be sent progressively to the browser in a format that can be easily parsed by JavaScript:
328
+
329
+
use App\Models\User;
330
+
331
+
Route::get('/users.json', function () {
332
+
return response()->streamJson([
333
+
'users' => User::cursor(),
334
+
]);
335
+
});
336
+
290
337
<aname="streamed-downloads"></a>
291
338
#### Streamed Downloads
292
339
@@ -300,15 +347,6 @@ Sometimes you may wish to turn the string response of a given operation into a d
300
347
->readme('laravel', 'laravel')['contents'];
301
348
}, 'laravel-readme.md');
302
349
303
-
<aname="file-responses"></a>
304
-
### File Responses
305
-
306
-
The `file` method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download. This method accepts the absolute path to the file as its first argument and an array of headers as its second argument:
0 commit comments