Skip to content

Commit 4c0a8fd

Browse files
authored
Merge pull request #53 from clue/output-buffer
Documentation for using output buffering
2 parents 0b35c22 + 8bee64b commit 4c0a8fd

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/api/response.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,74 @@ Last-Modified: Sat, 06 Nov 2021 12:31:04 GMT
383383
> for your response or don't want to expose this information, you may want to
384384
> use [`ETag`](#etag) headers from the previous section instead.
385385
386+
## Output buffering
387+
388+
PHP provides a number of functions that write directly to the
389+
[output buffer](https://www.php.net/manual/en/book.outcontrol.php) instead of
390+
returning values:
391+
392+
* [`echo`](https://www.php.net/manual/en/function.echo.php),
393+
[`print`](https://www.php.net/manual/en/function.print.php),
394+
[`printf()`](https://www.php.net/manual/en/function.printf.php),
395+
[`vprintf()`](https://www.php.net/manual/en/function.vprintf.php),
396+
etc.
397+
* [`var_dump()`](https://www.php.net/manual/en/function.var-dump.php),
398+
[`var_export()`](https://www.php.net/manual/en/function.var-export.php),
399+
[`print_r()`](https://www.php.net/manual/en/function.print-r.php),
400+
etc.
401+
* [`readfile()`](https://www.php.net/manual/en/function.readfile.php),
402+
[`fpassthru()`](https://www.php.net/manual/en/function.fpassthru.php),
403+
[`passthru()`](https://www.php.net/manual/en/function.passthru.php),
404+
etc.
405+
*
406+
407+
These functions can also be used in X, but do require some special care because
408+
we want to redirect this output to be part of an HTTP response instead. You can
409+
start a temporary output buffer using [`ob_start()`](https://www.php.net/manual/en/function.ob-start.php)
410+
to catch any output and return it as a response body like this:
411+
412+
```php
413+
<?php
414+
// …
415+
416+
$app->get('/dump', function () {
417+
ob_start();
418+
echo "Hello\n";
419+
var_dump(42);
420+
$body = ob_get_clean();
421+
422+
return new React\Http\Message\Response(
423+
200,
424+
['Content-Type' => 'text/plain; charset=utf-8'],
425+
$body
426+
);
427+
});
428+
```
429+
430+
An HTTP request can be sent like this:
431+
432+
```bash
433+
$ curl http://localhost:8080/dump
434+
Hello
435+
int(42)
436+
```
437+
438+
> ℹ️ **A word of caution**
439+
>
440+
> Special care should be taken if the code in question is deeply nested with
441+
> multiple return conditions or may throw an `Exception`.
442+
>
443+
> As a rule of thumb, output buffering should only be used as a last resort and
444+
> directly working with `string` values is usually preferable. For instance,
445+
> [`print_r()`](https://www.php.net/manual/en/function.print_r.php),
446+
> [`var_export()`](https://www.php.net/manual/en/function.var-export.php) and
447+
> others accept optional boolean flags to return the value instead of printing
448+
> to the output buffer. In many other cases, PHP also provides alternative
449+
> functions that directly return `string` values instead of writing to the output
450+
> buffer. For instance, instead of using
451+
> [`printf()`](https://www.php.net/manual/en/function.printf.php), you may want
452+
> to use [`sprintf()`](https://www.php.net/manual/en/function.sprintf.php).
453+
386454
## Internal Server Error
387455

388456
Each controller function needs to return a response object in order to send

0 commit comments

Comments
 (0)