By default, the security component stores information about the last request URI (for example, /en/admin ) in a session variable named _security.main.target_path ( main is the firewall name defined in security.yml ). After a successful login, the user is redirected to this path to help them continue working from the last known page they visited.
Note. No matter how many times the language changes on the login page, since the firewall always redirects to /en/admin/ after a successful login, so the locale changes to en again.
To fix this, you may need to change the default behavior of the target path :
Exception Listener Class:
// src/AppBundle/Security/Firewall/ExceptionListener.php use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener; class ExceptionListener extends BaseExceptionListener { use TargetPathTrait; protected function setTargetPath(Request $request) { if ($request->hasSession() && $request->isMethodSafe(false) && !$request->isXmlHttpRequest()) { $this->saveTargetPath( $request->getSession(), // the firewall name 'admin', // save the route name instead of the URI $request->attributes->get('_route') ); } } }
This generates an old route after logging in with the current locale.
Configuration:
For Symfony 2:
# app/config/services.yml parameters:
For Symfony 3:
You may need to create a compiler run and manually modify this class:
// src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php; class ExceptionListenerPass implements CompilerPassInterface { /** * {@inheritdoc} */ public function process(ContainerBuilder $container) { $definition = $container->getDefinition('security.exception_listener.admin'); $definition->setClass('AppBundle\Security\Firewall\ExceptionListener'); } }
Finally, register the compiler pass in your bundle:
// src/AppBundle/AppBundle.php class AppBundle extends Bundle { public function build(ContainerBuilder $container) { $container->addCompilerPass(new ExceptionListenerPass()); } }
source share