Is it possible to set a session cookie, upon successful login? An event listener? If so, which event? And how i can access the response object to attach the cookie to it?
You can create an EventListener which listens to the kernel.response Event and modify the response.
namespace Acme\DemoBundle\EventListener
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Cookie;
class AddMyCookie
{
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
if (/** whatever **/) {
$response->headers->setCookie(new Cookie('cookie_name', 'cookie_value'));
}
}
}
in your services.yml:
parameters:
acme_demo.add_my_cookie.class: Acme\DemoBundle\EventListener\AddMyCookie
services:
acme_demo.add_my_cookie.kernel_response_listener:
class: %acme_demo.add_my_cookie.class%
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
Whatever you need to decide to set the cookie (security_context, usermanager, session, etc.) can be injected trough the DI Container.
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