我正在使用FOSUserBundle软件包使用Symfony 2构建Web应用程序.
用户创建帐户,登录并开始使用该应用程序.
我现在想要实现的是让用户从他们登录的任何页面重定向到他们的帐户.
这包括:
基本上代码是这样的:
$container = $this->container;
$accountRouteName = "DanyukiWebappBundle_account";
if( $container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
// authenticated (NON anonymous)
$routeName = $container->get('request')->get('_route');
if ($routeName != $accountRouteName) {
return $this->redirect($this->generateUrl($accountRouteName));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道该代码应该去哪里.
它应该针对任何请求执行.在Symfony 1中我会使用过滤器.
dan*_*dan 31
我自己找到了解决方案:
<?php
namespace Danyuki\UserBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LoggedInUserListener
{
private $router;
private $container;
public function __construct($router, $container)
{
$this->router = $router;
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
$container = $this->container;
$accountRouteName = "DanyukiWebappBundle_account";
if( $container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
// authenticated (NON anonymous)
$routeName = $container->get('request')->get('_route');
if ($routeName != $accountRouteName) {
$url = $this->router->generate($accountRouteName);
$event->setResponse(new RedirectResponse($url));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的bundle的services.yml文件中:
services:
kernel.listener.logged_in_user_listener:
class: Danyuki\UserBundle\Listener\LoggedInUserListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [ @router, @service_container ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20445 次 |
| 最近记录: |