1

我按照本教程设置了登录后到最后一页的重定向: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

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

4

2 回答 2

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' }
于 2013-03-29T08:23:50.367 回答
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”设置

于 2013-04-03T04:19:04.233 回答