Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpFoundation] added support for streamed responses #2935

Merged
merged 5 commits into from Dec 31, 2011
Merged

Commits on Dec 21, 2011

  1. [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.
    fabpot committed Dec 21, 2011
    Copy the full SHA
    0038d1b View commit details
    Browse the repository at this point in the history
  2. made some cosmetic changes

    fabpot committed Dec 21, 2011
    Copy the full SHA
    e44b8ba View commit details
    Browse the repository at this point in the history
  3. Copy the full SHA
    8717d44 View commit details
    Browse the repository at this point in the history

Commits on Dec 22, 2011

  1. Copy the full SHA
    473741b View commit details
    Browse the repository at this point in the history

Commits on Dec 31, 2011

  1. Copy the full SHA
    887c0e9 View commit details
    Browse the repository at this point in the history