10

Since I've read that Chrome is having trouble looping an HTML5 video if the response code isn't 206, I'd like to render my template with a 206 code.

Yet I've not found anywhere how to specify an html code when rendering a template... Did anyone already tried that and succeeded ?

jherax
  • 5,238
  • 5
  • 38
  • 50
Michael De Keyser
  • 787
  • 1
  • 17
  • 45
  • I don't know why that's different but that problem solved itself when I put my app into test/prod env. Suddenly, the video is looping as it should. – Michael De Keyser Mar 13 '15 at 14:38

4 Answers4

7

In the controller you can create and return a Response object with the content and the specified return code, as example:

return new Response(
    $this->renderView('AcmeDemoBundle:Default:video.html.twig', array(
            'param1'    => $param1,
            'param2'     => $param2,
        )),
        206  // return code
);

Hope this help

Egg
  • 1,782
  • 1
  • 12
  • 28
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • 2
    This indeed works syntactically. I can see in the dev tool bar that the response status is indeed 206 but I still have that "HTTP/1.0 200 OK Cache-Control: no-cache Date: Thu, 05 Mar 2015 18:45:02 GMT" just under my element when I inspect the source code with chrome's tool... Does it have any incidence ? – Michael De Keyser Mar 05 '15 at 18:47
  • 1
    I'm also getting this header prepended to the response body – Pierre de LESPINAY Jul 02 '15 at 09:57
  • Hi @PierredeLESPINAY have you tried in the (sf) prod environment? check the author comment on the question itself. Let me know – Matteo Jul 02 '15 at 10:05
6

You can pass a response object with your renderResponse that has the necessary status code.

$response = new Response('', 206);

return $this->renderResponse(
// Or return $this->container->get('templating')
    'AcmeBundle:Video:show.html.twig',
    array('video' => video),
    $response
);

If you do not pass in a Response with your renderResponse one will be generated automatically. If you pass one then it's content is just set to that of the rendered template (as you can see in the code)

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • This should be the correct answer. You can also use the `render` method directly: `public function render($view, array $parameters = array(), Response $response = null)`. So the call would be: `return $this->render('AcmeBundle:Video:show.html.twig', array('video' => video), new Response('', 206));` – StockBreak Dec 18 '15 at 09:28
4

New implementation

protected function renderError(array $parameters, $statusCode = 500)
{
    return $this->render(
        'default/error.html.twig',
        $parameters,
        new Response('', $statusCode)
    );
}
Gadelkareem
  • 1,067
  • 13
  • 30
1

I think I you at this before you are rendering the template you will get the desired result:

$this->getContext()->getResponse()->setStatusCode(206);    

btw.
The class Symfony\Component\HttpFoundation\Response provides constants for all valid HTTP-states

Nickolaus
  • 4,785
  • 4
  • 38
  • 60