本文介绍了Symfony2 动态注销目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以正常登录和退出用户的 Symfony2 应用程序,当退出时,它可以正确地将用户重定向到主页.

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.

我想在退出时将它们保留在当前页面上,但没有其登录权限.

I'd like to keep them on their current page when the log out, only without their logged-in privileges.

我的问题是:

我可以动态设置用户退出时被定向到的页面吗?

Can I dynamically set the page the user is directed to when they log out?

推荐答案

我需要一个 Logout Success Handler,我是这样实现的:

I needed a Logout Success Handler, and this is how I implemented it:

security.yml:

logout:
    success_handler: acme.security.logout_success_handler

config.yml:

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

Symfony/src/Acme/DefaultBundle/Handler/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);
    }
}

这篇关于Symfony2 动态注销目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:56