src/EventListener/RequestListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\Security\Core\Security;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. use App\Repository\UserRepository;
  7. class RequestListener
  8. {
  9.     private $security;
  10.     private $repo_user;
  11.     private $translator;
  12.     public function __construct(UserRepository $repo_userSecurity $securityTranslatorInterface $translator)
  13.     {
  14.         $this->repo_user $repo_user;
  15.         $this->security $security;
  16.         $this->translator $translator;
  17.     }
  18.     public function onKernelRequest(RequestEvent $event)
  19.     {
  20.         $request $event->getRequest();
  21.         // The isMainRequest() method was introduced in Symfony 5.3.
  22.         // In previous versions it was called isMasterRequest()
  23.         if ($event->isMainRequest()) {
  24.           $logged_user $this->security->getUser();
  25.           if(!is_null($logged_user)) {
  26.             $request->setLocale($logged_user->getLangage());
  27.             $this->translator->setLocale($logged_user->getLangage());
  28.           }
  29.         }
  30.         // ...
  31.     }
  32. }
  33. ?>