Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render a template with a specific status code

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 ?

like image 513
Michael De Keyser Avatar asked Mar 05 '15 16:03

Michael De Keyser


2 Answers

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

like image 70
Matteo Avatar answered Sep 29 '22 08:09

Matteo


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)

like image 42
qooplmao Avatar answered Sep 29 '22 07:09

qooplmao