{"issue":{"user":{"id":47313,"login":"fabpot","url":"https://api.github.com/users/fabpot","gravatar_id":"f1a2c5905e121d09feba5ad73898ffca","avatar_url":"https://secure.gravatar.com/avatar/f1a2c5905e121d09feba5ad73898ffca?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"milestone":null,"title":"[HttpFoundation] added support for streamed responses","created_at":"2011-12-21T13:37:32Z","updated_at":"2011-12-21T16:19:48Z","labels":[],"state":"open","id":2624988,"html_url":"https://github.com/symfony/symfony/issues/2935","closed_at":null,"pull_request":{"patch_url":"https://github.com/symfony/symfony/pull/2935.patch","html_url":"https://github.com/symfony/symfony/pull/2935","diff_url":"https://github.com/symfony/symfony/pull/2935.diff"},"number":2935,"body":"To stream a Response, use the StreamedResponse class instead of the\r\nstandard Response class:\r\n\r\n    $response = new StreamedResponse(function () {\r\n        echo 'FOO';\r\n    });\r\n\r\n    $response = new StreamedResponse(function () {\r\n        echo 'FOO';\r\n    }, 200, array('Content-Type' => 'text/plain'));\r\n\r\nAs you can see, a StreamedResponse instance takes a PHP callback instead of\r\na string for the Response content. It's up to the developer to stream the\r\nresponse content from the callback with standard PHP functions like echo.\r\nYou can also use flush() if needed.\r\n\r\nFrom a controller, do something like this:\r\n\r\n    $twig = $this->get('templating');\r\n\r\n    return new StreamedResponse(function () use ($templating) {\r\n        $templating->stream('BlogBundle:Annot:streamed.html.twig');\r\n    }, 200, array('Content-Type' => 'text/html'));\r\n\r\nIf you are using the base controller, you can use the stream() method instead:\r\n\r\n    return $this->stream('BlogBundle:Annot:streamed.html.twig');\r\n\r\nYou can stream an existing file by using the PHP built-in readfile() function:\r\n\r\n    new StreamedResponse(function () use ($file) {\r\n        readfile($file);\r\n    }, 200, array('Content-Type' => 'image/png');\r\n\r\nRead http://php.net/flush for more information about output buffering in PHP.\r\n\r\nNote that you should do your best to move all expensive operations to\r\nbe \"activated/evaluated/called\" during template evaluation.\r\n\r\nTemplates\r\n---------\r\n\r\nIf you are using Twig as a template engine, everything should work as\r\nusual, even if are using template inheritance!\r\n\r\nHowever, note that streaming is not supported for PHP templates. Support\r\nis impossible by design (as the layout is rendered after the main content).\r\n\r\nExceptions\r\n----------\r\n\r\nExceptions thrown during rendering will be rendered as usual except that\r\nsome content might have been rendered already.\r\n\r\nLimitations\r\n-----------\r\n\r\nAs the getContent() method always returns false for streamed Responses, some\r\nevent listeners won't work at all:\r\n\r\n* Web debug toolbar is not available for such Responses (but the profiler works fine);\r\n* ESI is not supported.\r\n\r\nAlso note that streamed responses cannot benefit from HTTP caching for obvious\r\nreasons.","url":"https://api.github.com/repos/symfony/symfony/issues/2935","comments":3,"assignee":null},"comment":{"user":{"id":135607,"login":"johnkary","url":"https://api.github.com/users/johnkary","gravatar_id":"d3e3f4ac37c02289f5dfed115949fc88","avatar_url":"https://secure.gravatar.com/avatar/d3e3f4ac37c02289f5dfed115949fc88?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"created_at":"2011-12-21T16:19:48Z","updated_at":"2011-12-21T16:19:48Z","id":3235716,"body":"@cordoval Common use cases are present fairly well by this RailsCast video: http://railscasts.com/episodes/266-http-streaming\r\n\r\nEssentially it allows faster fetching of web assets (JS, CSS, etc) located in the <head></head>, allowing those assets to be fetched as soon as possible before the remainder of the content body is computed and sent to the browser. The end goal is to improve page load speed.","url":"https://api.github.com/repos/symfony/symfony/issues/comments/3235716"},"action":"created"}