0038d1bac4
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.
44 lines
2.2 KiB
XML
44 lines
2.2 KiB
XML
<?xml version="1.0" ?>
|
|
|
|
<container xmlns="http://symfony.com/schema/dic/services"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
|
|
|
<parameters>
|
|
<parameter key="controller_resolver.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver</parameter>
|
|
<parameter key="controller_name_converter.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser</parameter>
|
|
<parameter key="response_listener.class">Symfony\Component\HttpKernel\EventListener\ResponseListener</parameter>
|
|
<parameter key="streamed_response_listener.class">Symfony\Component\HttpKernel\EventListener\StreamedResponseListener</parameter>
|
|
<parameter key="locale_listener.class">Symfony\Component\HttpKernel\EventListener\LocaleListener</parameter>
|
|
</parameters>
|
|
|
|
<services>
|
|
<service id="controller_name_converter" class="%controller_name_converter.class%" public="false">
|
|
<tag name="monolog.logger" channel="request" />
|
|
<argument type="service" id="kernel" />
|
|
</service>
|
|
|
|
<service id="controller_resolver" class="%controller_resolver.class%" public="false">
|
|
<tag name="monolog.logger" channel="request" />
|
|
<argument type="service" id="service_container" />
|
|
<argument type="service" id="controller_name_converter" />
|
|
<argument type="service" id="logger" on-invalid="ignore" />
|
|
</service>
|
|
|
|
<service id="response_listener" class="%response_listener.class%">
|
|
<tag name="kernel.event_subscriber" />
|
|
<argument>%kernel.charset%</argument>
|
|
</service>
|
|
|
|
<service id="streamed_response_listener" class="%streamed_response_listener.class%">
|
|
<tag name="kernel.event_subscriber" />
|
|
</service>
|
|
|
|
<service id="locale_listener" class="%locale_listener.class%">
|
|
<tag name="kernel.event_subscriber" />
|
|
<argument>%kernel.default_locale%</argument>
|
|
<argument type="service" id="router" on-invalid="ignore" />
|
|
</service>
|
|
</services>
|
|
</container>
|