src/EventSubscriber/RedirectToLocaleSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. /**
  9.  * When visiting the homepage, this listener redirects the user to the most
  10.  * appropriate localized version according to the browser settings.
  11.  *
  12.  * See http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-request-event
  13.  *
  14.  * @author Oleg Voronkovich <oleg-voronkovich@yandex.ru>
  15.  */
  16. class RedirectToLocaleSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var UrlGeneratorInterface
  20.      */
  21.     private $urlGenerator;
  22.     /**
  23.      * List of supported locales.
  24.      *
  25.      * @var string[]
  26.      */
  27.     private $locales = [];
  28.     /**
  29.      * @var string
  30.      */
  31.     private $defaultLocale '';
  32.     /**
  33.      * @param string      $locales       Supported locales separated by '|'
  34.      * @param string|null $defaultLocale
  35.      */
  36.     public function __construct(UrlGeneratorInterface $urlGenerator$locales$defaultLocale null)
  37.     {
  38.         $this->urlGenerator $urlGenerator;
  39.         $this->locales explode('|'trim($locales));
  40.         if (empty($this->locales)) {
  41.             throw new \UnexpectedValueException('The list of supported locales must not be empty.');
  42.         }
  43.         $this->defaultLocale $defaultLocale ?: $this->locales[0];
  44.         if (!in_array($this->defaultLocale$this->locales)) {
  45.             throw new \UnexpectedValueException(sprintf('The default locale ("%s") must be one of "%s".'$this->defaultLocale$locales));
  46.         }
  47.         // Add the default locale at the first position of the array,
  48.         // because Symfony\HttpFoundation\Request::getPreferredLanguage
  49.         // returns the first element when no an appropriate language is found
  50.         array_unshift($this->locales$this->defaultLocale);
  51.         $this->locales array_unique($this->locales);
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             KernelEvents::REQUEST => ['onKernelRequest'],
  57.         ];
  58.     }
  59.     public function onKernelRequest(RequestEvent $event)
  60.     {
  61.         $request $event->getRequest();
  62.         // Ignore sub-requests and all URLs but the homepage
  63.         if ('/admin' !== $request->getPathInfo()) {
  64.             return;
  65.         }
  66.         // Ignore requests from referrers with the same HTTP host in order to prevent
  67.         // changing language for users who possibly already selected it for this application.
  68.         if (=== stripos($request->headers->get('referer'), $request->getSchemeAndHttpHost())) {
  69.             return;
  70.         }
  71.         $preferredLanguage $request->getPreferredLanguage($this->locales);
  72.         $response = new RedirectResponse($this->urlGenerator->generate('app_welcome', ['_locale' => $preferredLanguage]));
  73.         $event->setResponse($response);
  74.     }
  75. }