简体   繁体   English

Symfony2动态注销目标?

[英]Symfony2 Dynamic Logout Target?

I have a working Symfony2 application that properly logs users in and out, and when logging out it properly redirects the user to the home page. 我有一个工作的Symfony2应用程序,可以正确地记录和退出用户,并在正确注销时将用户重定向到主页。

I'd like to keep them on their current page when the log out, only without their logged-in privileges. 我希望在注销时将它们保留在当前页面上,只是没有他们的登录权限。

My question is: 我的问题是:

Can I dynamically set the page the user is directed to when they log out? 可以动态设置用户注销时指向的页面吗?

What you need is a logout success handler. 您需要的是注销成功处理程序。

Define the logout handler in the security.yml: 在security.yml中定义注销处理程序:

security:
    firewalls:
        admin_area:
            logout:
                success_handler: acme.security.logout_success_handler

And the handler goes like this: 处理程序是这样的:

namespace Acme\Bundle\SecurityBundle\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\DependencyInjection\ContainerAware;

class LogoutSuccessHandler extends ContainerAware implements LogoutSuccessHandlerInterface
    {
    public function onLogoutSuccess(Request $request)
    {
        // dynamic route logic

        return new RedirectResponse($this->container->get('router')->generate('dynamic_route_name'));
    }
}

Btw... Please remove the unwanted imports and Hope this helps! 顺便说一句...请删除不需要的进口,希望这有帮助! :D :d

Here is the services.yml 这是services.yml

services:
    acme.security.logout_success_handler:
        class: Acme\Bundle\SecurityBundle\Handler\LogoutSuccessHandler
        calls:
            - [ setContainer, [ @service_container ] ]

I needed a Logout Success Handler, and this is how I implemented it: 我需要一个Logout Success Handler,这就是我实现它的方式:

security.yml : security.yml

logout:
    success_handler: acme.security.logout_success_handler

config.yml : config.yml

services:
    acme.security.logout_success_handler:
        class: Acme\DefaultBundle\Handler\LogoutSuccessHandler

Symfony/src/Acme/DefaultBundle/Handler/LogoutSuccessHandler.php: Symfony的/ src目录/阿克米/ DefaultBundle /处理器/ LogoutSuccessHandler.php:

<?php

namespace Acme\DefaultBundle\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;

class LogoutSuccessHandler extends ContainerAware implements LogoutSuccessHandlerInterface
{
    public function onLogoutSuccess(Request $request)
    {
        $target_url = $request->query->get('target_url')
                      ? $request->query->get('target_url')
                      : "/";
        return new RedirectResponse($target_url);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM