Attaching a cookie to a view in Symfony2

前端 未结 1 1006
眼角桃花
眼角桃花 2021-02-09 11:57

I\'ve found a few questions and pages dealing with cookies in Symfony2 but there doesn\'t seem to be any clear consensus on exactly how this is supposed to work. I can, of cour

相关标签:
1条回答
  • 2021-02-09 12:11

    I think you're on the right lines with:

    $response->headers->setCookie(new Cookie('name', 'value', 0, '/'));
    

    If you're trying to render a template then check out the docs here:

    Symfony2 Templating Service

    If you look at the line:

    return $this->render('AcmeArticleBundle:Article:index.html.twig');
    

    basically the render method is returning a response (which the controller then returns) which has the content of the twig template, all you have to do is intercept this:

    $response = $this->render('AcmeArticleBundle:Article:index.html.twig');
    $response->headers->setCookie(new Cookie('name', 'value', 0, '/'));
    return $response;
    

    I think that's it anyway...

    0 讨论(0)
提交回复
热议问题