我按照本教程设置了登录后到最后一页的重定向:http ://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/
我的服务.yml
parameters:
assinatura_user_security.component.login_success_handler.class: Assinatura\UserBundle\Component\LoginSuccessHandler
services:
assinatura_user_security.component.login_success_handler:
class: %assinatura_user_security.component.login_success_handler.class%
arguments: [@service_container, @router, @security.context]
tags:
- { name: 'monolog.logger', channel: 'security' }
安全.yml
form_login:
login_path: usuario_login
check_path: usuario_login_check
success_handler: assinatura_user_security.component.login_success_handler
我的课:
<?php
namespace Assinatura\UserBundle\Component;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
public function __construct(Router $router, SecurityContext $security)
{
$this->router = $router;
$this->security = $security;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
return $response;
}
}
和错误:
可捕获的致命错误:传递给 Assinatura\UserBundle\Component\LoginSuccessHandler::__construct() 的参数 1 必须是 Symfony\Component\Routing\Router 的实例,给定 appDevDebugProjectContainer 的实例,在 /var/www/assinatura/app/cache 中调用/dev/appDevDebugProjectContainer.php 在第 131 行并在 /var/www/assinatura/src/Assinatura/UserBundle/Component/LoginSuccessHandler.php 第 18 行中定义
我的 symfony 版本是 2.2
我想在未登录时重定向到同一页面