<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
use Symfony\Component\HttpFoundation\RequestStack;
class SetUserLangSubscriber implements EventSubscriberInterface
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
AuthenticationSuccessEvent::class => 'onAuthenticationSuccess',
];
}
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
// get user's lang setting and save it in the session, so LocaleListener can get it
$user = $event->getAuthenticationToken()->getUser();
$session = $this->requestStack->getSession();
$session->set('user_lang', $user->getLang());
}
}