src/EventSubscriber/AdminSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\Routing\RouterInterface;
  7. class AdminSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(protected readonly RouterInterface $router)
  10.     {
  11.     }
  12.     public const ADMIN_USER_LOCALE 'admin_user_locale';
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             KernelEvents::REQUEST => ['onRequest'20],
  17.         ];
  18.     }
  19.     public function onRequest(RequestEvent $event)
  20.     {
  21.         $request $event->getRequest();
  22.         if (!str_starts_with($request->getPathInfo(), '/admin')) {
  23.             return;
  24.         }
  25.         if ($request->getSession()->has(self::ADMIN_USER_LOCALE)) {
  26.             $locale $request->getSession()->get(self::ADMIN_USER_LOCALE);
  27.             $request->attributes->set('_locale'$locale);
  28.             $request->setLocale($locale);
  29.             $request->setDefaultLocale($locale);
  30.         }
  31.         $this->router->getContext()->setParameter('IS_ADMIN'true);
  32.     }
  33. }