I found a few cookie questions and pages on Symfony2, but there seems to be no clear consensus on how this should work. Of course, I can return to using the built-in PHP function setcookie, but I feel that with Symfony2 it should be easy too.
I have an action in my controller from which I just want to return the view with the cookie attached. Until now, it seemed to me that basically such examples:
use Symfony\Compentnt\HttpFoundation\Response;
public function indexAction() {
$response = new Response();
$response->headers->setCookie(new Cookie('name', 'value', 0, '/');
$response->send();
}
The problem is that it sends a response ... and does not display the view. If I set a cookie without sending headers, the view will be rendered, but the heading (cookie) will not be sent.
Shaking, I found the method sendHeaders()in the Response object, so now I manually call it in my action before returning and it seems to work:
public function indexAction() {
...
$response->sendHeaders();
return array('variables' => 'values');
}
But is this really the expected model? In previous versions of symfony, I could set headers in my controller and expect the view controller to process sending all the messages I sent. Now it seems that I have to manually send them from the action to make it work, which means that I have to call it from any action in which I set the headers. Is this true or is there something that I am missing that is so obvious that no one even bothered to mention this in any documentation?
source
share