This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpFoundation
Fabien Potencier 0038d1bac4 [HttpFoundation] added support for streamed responses
To stream a Response, use the StreamedResponse class instead of the
standard Response class:

    $response = new StreamedResponse(function () {
        echo 'FOO';
    });

    $response = new StreamedResponse(function () {
        echo 'FOO';
    }, 200, array('Content-Type' => 'text/plain'));

As you can see, a StreamedResponse instance takes a PHP callback instead of
a string for the Response content. It's up to the developer to stream the
response content from the callback with standard PHP functions like echo.
You can also use flush() if needed.

From a controller, do something like this:

    $twig = $this->get('templating');

    return new StreamedResponse(function () use ($templating) {
        $templating->stream('BlogBundle:Annot:streamed.html.twig');
    }, 200, array('Content-Type' => 'text/html'));

If you are using the base controller, you can use the stream() method instead:

    return $this->stream('BlogBundle:Annot:streamed.html.twig');

You can stream an existing file by using the PHP built-in readfile() function:

    new StreamedResponse(function () use ($file) {
        readfile($file);
    }, 200, array('Content-Type' => 'image/png');

Read http://php.net/flush for more information about output buffering in PHP.

Note that you should do your best to move all expensive operations to
be "activated/evaluated/called" during template evaluation.

Templates
---------

If you are using Twig as a template engine, everything should work as
usual, even if are using template inheritance!

However, note that streaming is not supported for PHP templates. Support
is impossible by design (as the layout is rendered after the main content).

Exceptions
----------

Exceptions thrown during rendering will be rendered as usual except that
some content might have been rendered already.

Limitations
-----------

As the getContent() method always returns false for streamed Responses, some
event listeners won't work at all:

* Web debug toolbar is not available for such Responses (but the profiler works fine);
* ESI is not supported.

Also note that streamed responses cannot benefit from HTTP caching for obvious
reasons.
2011-12-21 14:34:26 +01:00
..
File merged 2.0 2011-12-13 16:12:53 +01:00
SessionStorage fixed CS 2011-12-18 14:36:25 +01:00
ApacheRequest.php [HttpFoundation] fixed a small bug on Windows 2011-03-17 12:28:29 +01:00
composer.json bumped Symfony version in composer.json files to 2.0.7 2011-11-17 06:58:47 +01:00
Cookie.php [HttpFoundation] Cookie values should not be restricted 2011-11-23 11:38:46 +01:00
FileBag.php [DoctrineBridge] fixed some CS 2011-12-13 10:22:12 +01:00
HeaderBag.php [HttpFoundation] tagged public @api 2011-07-20 10:06:02 +02:00
LICENSE added LICENSE files for the subtree repositories 2011-02-22 18:58:15 +01:00
ParameterBag.php merged 2.0 2011-12-18 14:48:17 +01:00
README.md tweaked the README files 2011-12-18 14:22:28 +01:00
RedirectResponse.php [RedirectResponse] Added missing doctype and title tag 2011-08-24 05:31:42 +03:00
Request.php fixed CS 2011-12-18 14:36:25 +01:00
RequestMatcher.php fixed CS 2011-12-18 14:36:25 +01:00
RequestMatcherInterface.php [HttpFoundation] tagged public @api 2011-07-20 10:06:02 +02:00
Response.php fixed CS 2011-12-18 14:36:25 +01:00
ResponseHeaderBag.php fixed CS 2011-12-18 14:36:25 +01:00
ServerBag.php fixed CS 2011-12-18 14:36:25 +01:00
Session.php moved management of the locale from the Session class to the Request class 2011-10-08 18:34:49 +02:00
StreamedResponse.php [HttpFoundation] added support for streamed responses 2011-12-21 14:34:26 +01:00

HttpFoundation Component

HttpFoundation defines an object-oriented layer for the HTTP specification.

It provides an abstraction for requests, responses, uploaded files, cookies, sessions, ...

In this example, we get a Request object from the current PHP global variables:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();
echo $request->getPathInfo();

You can also create a Request directly -- that's interesting for unit testing:

$request = Request::create('/?foo=bar', 'GET');
echo $request->getPathInfo();

And here is how to create and send a Response:

$response = new Response('Not Found', 404, array('Content-Type' => 'text/plain'));
$response->send();

The Request and the Response classes have many other methods that implement the HTTP specification.

Resources

Unit tests:

https://github.com/symfony/symfony/tree/master/tests/Symfony/Tests/Component/HttpFoundation