I'm having an issue with a symfony 4.4 website and the _locale parameter.
Let's suppose the website url is example.comThere's a prefix to every controller to set the _locale parameters, the default locale being en.The home page example.com works fine.Any url like example.com/locale/xxx works fine.
But every url like example.com/locale is rewrote as example.comlocale which leads to an error.So example.com/en becomes example.comen/example.com/frbecomes example.comfr/ and so on...
It's pretty annoying, can somebody helps me with this please?
Here's my setup:
In annotations.yaml for controllers route :
controllers: resource: ../../src/Controller/ type: annotation prefix: /{_locale} requirements: _locale: '%app_locales%' defaults: _locale: '%locale%'
In services.yaml my parameters are:
parameters: locale: 'fr' #WM app_locales: fr|en| uploads_dir_name: 'uploads' uploads_base_url: '%env(SITE_BASE_URL)%/%uploads_dir_name%'
Note : the | at the end in fr|en| is needed, the entire rewriting doesn't work without it.
The index controller route:
/** * @Route("/", name="app_home") */public function index(Request $request)
The listener in charge of the locale:
class LocaleSubscriber implements EventSubscriberInterface{ private $defaultLocale; public function __construct($defaultLocale = 'en') { $this->defaultLocale = $defaultLocale; } public function onKernelRequest(RequestEvent $event) { $request = $event->getRequest(); if (!$request->hasPreviousSession()) { return; } // try to see if the locale has been set as a _locale routing parameter if ($locale = $request->attributes->get('_locale')) { $request->getSession()->set('_locale', $locale); } else { // if no explicit locale has been set on this request, use one from the session $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); } } public static function getSubscribedEvents() { return [ // must be registered before (i.e. with a higher priority than) the default Locale listener KernelEvents::REQUEST => [['onKernelRequest', 20]], ]; }}
Thank you in advance for your help!