在构造函数参数上登录重定向服务句柄错误

时间:2013-03-29 00:38:36

标签: symfony service symfony-2.2

我按照本教程设置了一个重定向到登录后的最后一页:http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

我的services.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' }

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;
    }

}

和错误:

  

捕获致命错误:参数1传递给   Assinatura \ UserBundle \分量\ LoginSuccessHandler :: __结构()   必须是Symfony \ Component \ Routing \ Router的实例,实例   appDevDebugProjectContainer给出,调用   /var/www/assinatura/app/cache/dev/appDevDebugProjectContainer.php on   第131行并在中定义   /var/www/assinatura/src/Assinatura/UserBundle/Component/LoginSuccessHandler.php   第18行

我的symfony版本是2.2

我想在未记录时重定向到同一页面

2 个答案:

答案 0 :(得分:3)

尝试在services.yml中从arguments: [@service_container, @router, @security.context]更改为arguments: [@router, @security.context]

services:
     assinatura_user_security.component.login_success_handler:
        class:  %assinatura_user_security.component.login_success_handler.class%
        arguments:  [@router, @security.context]
        tags:
            - { name: 'monolog.logger', channel: 'security' }

答案 1 :(得分:0)

我找到了这个解决方案(2.1中的新内容)

frontend:
            pattern:        ^/*
            provider:       usuarios
            anonymous:      ~
            form_login:
                login_path: usuario_login
                check_path: usuario_login_check
                use_referer:        true
            logout:
                path:       usuario_logout
            remember_me:
                key:        userassinatura2013
                lifetime:   604800 

'use_referer'设置为在登录后用于重定向