登录重定向前的 Symfony

Symfony before login redirect

我想在 Symfony 将我重定向到登录页面之前存储 POST 参数(如果我没有登录)。

我尝试在请求上设置侦听器,但这不起作用。 我正在使用 FOSUserBundle,但是没有事件可以监听这个重定向。

有人可以帮我解决这个问题吗?

在您的 security.yml 文件中,您可以声明此

form_login:
           // All others options that you need
           failure_handler: redirect_after_login_failed

在 services.yml

中像这样声明您的服务
redirect_after_login_failed:
    class: AppBundle\Redirection\AfterLoginFailedRedirection
    arguments: ["@router"]

在你身上AppBundle/Redirection

namespace AppBundle\Redirection;

class AfterLoginRedirection implements AuthenticationFailureHandlerInterface {

    /**
     * @var $router \Symfony\Component\Routing\RouterInterface
     */
    private $router;

    public function __construct(RouterInterface $router) {
        $this->router = $router;
    }


    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        //Do what you want here

        //Redirect where you want
        $redirection = new RedirectResponse($this->router->generate(''));
        return $redirection;
    }

}
  • PS : 您需要添加 use 语句

如果用户被您的安全系统拒绝并且在他被重定向到您的登录页面之前,这允许您做一些事情。

找到了。

我将请求侦听器的优先级更改为 9。 (Symfony 防火墙设置为 8)。 非常有用的命令:php app/console debug:event-dispatcher

希望这对某人有所帮助。