Reputation: 1382
How do I set a cookie in a listener without interrupting other things and redirects?
I'm trying to set it on an InteractiveLoginEvent. I tried to set it like that:
$response = new Response();
$response->headers->setCookie(new Cookie('foo', 'bar'));
$response->send();
Cookie is set this way but after that I get blank page and it's not redirecting to the landing page after login. how do I solve this?
Upvotes: 1
Views: 2283
Reputation: 274
I use setcookie() when I need to deal with POST variables, which it seems to be not possible with RedirectResponse.
Upvotes: 2
Reputation: 1506
What if you just use PHP's method setcookie() instead of generating a redirect?
When you have a redirects the page might trigger endless redirect errors (depends on what you do) so if you just use this method then you are OK and your cookie is going to be set properly.
Upvotes: 1
Reputation: 2694
Try using RedirectResponse:
$response = new RedirectResponse('url_to_redirect_to');
$response->headers->setCookie(new Cookie('foo', 'bar'));
$response->send();
It will set a cookie and redirect to url you specify. Also consider injecting Router
into your listener to generate proper url.
Upvotes: 1