I'm trying to set a cookie in a Controller before rendering a twig file. After trying out a few suggestions i saw here, my code looks like below:
public function demandAction() {
$result = array('message' => '');
$response = $this->render('MainBundle:Default:demand.html.twig', $result);
$response->headers->setCookie(new Cookie('cookie', 'value', time() + 3600 * 24 * 7));
return $response;
}
But the cookie is not available in my rendered page when i alert document.cookie. I'm sure i must have missed something. Please help me out. Thanks.
The cookie object has httpOnly set to true by default, http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Cookie.html
This means that the browser should not make the cookie visible to client-side scripts. If you need to see the cookie in your scripts you can pass the 7th parameter as false when you create the cookie.
$response->headers->setCookie(new Cookie('foo', 'bar',time() + 60, '/', null, false, false));
If you just need to view the cookie for debugging purposes you can use Chrome Dev tools. They are available under the 'Resources' tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With