tun*_*ten 2 php event-listener symfony
在Symfony 3.4.0中,从一个侦听InteractiveLoginEvent我想要重定向到另一个路由的事件监听器,所以我想响应一个重定向.
但该InteractiveLoginEvent对象没有"setResponse"方法.那么使用它InteractiveLoginEvent来修改响应以便将重定向发送给用户的首选方法是什么?
要在成功登录后执行重定向,您可以添加自定义成功处理程序,该处理程序应实现AuthenticationSuccessHandlerInterface.
AuthenticationSuccessHandler.php
<?php
namespace AppBundle\Security\Authentication\Handler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
return new RedirectResponse($this->router->generate('homepage'));
}
}
Run Code Online (Sandbox Code Playgroud)
services.yml
services:
AppBundle\Security\Authentication\Handler:
arguments: ['@router']
Run Code Online (Sandbox Code Playgroud)
security.yml
security:
# ...
firewalls:
form_login:
# ...
use_referer: false
success_handler: AppBundle\Security\Authentication\Handler\AuthenticationSuccessHandler
Run Code Online (Sandbox Code Playgroud)