我有一个可以正常登录和退出用户的Symfony2应用程序,当正确登出时,会将用户重定向到主页。
我希望在注销时将它们保留在当前页面上,只是没有他们的登录权限。
我的问题是:
我可以动态设置用户退出时指向的页面吗?
答案 0 :(得分:8)
您需要的是退出成功处理程序。
在security.yml中定义logout处理程序:
security:
firewalls:
admin_area:
logout:
success_handler: acme.security.logout_success_handler
处理程序是这样的:
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'));
}
}
顺便说一句......请删除不需要的进口并希望这有帮助! :d
这是services.yml
services:
acme.security.logout_success_handler:
class: Acme\Bundle\SecurityBundle\Handler\LogoutSuccessHandler
calls:
- [ setContainer, [ @service_container ] ]
答案 1 :(得分:4)
我需要一个Logout Success Handler,这就是我实现它的方式:
<强> 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 /处理器/ 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);
}
}