FOSUserBundle:登录后进行注册重定向 [英] FOSUserBundle: Registration redirect if logged in
本文介绍了FOSUserBundle:登录后进行注册重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如果重定向尝试访问注册页面的用户的最佳方法是什么?
I was wondering what would be the best method to redirect a user who is ALREADY LOGGED IN if they try and access the registration page.
我希望有一种方法,该方法不需要我仅为了实现登录检查和检查而覆盖注册控制器.重定向.
I would prefer a method that does not require me overriding the registration controller simply to implement a login check & redirect.
我查看了注册初始化事件,但是我不知道如何启动重定向,因为似乎没有办法通过UserEvent类设置事件响应.
I looked at the registration initialization event but I don't know how to initiate the redirect since there doesn't seem to be a way to set the event response via the UserEvent class.
谢谢
推荐答案
我使用类似以下的内容.
I use something like the following..
namespace Acme\UserBundle\EventSubscriber;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
class FOSUserSubscriber implements EventSubscriberInterface
{
protected $router;
protected $securityContext;
public function __construct(
UrlGeneratorInterface $router,
SecurityContextInterface $securityContext
)
{
$this->router = $router;
$this->securityContext = $securityContext;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_INITIALIZE => 'forwardToRouteIfUser',
);
}
public function forwardToRouteIfUser(GetResponseUserEvent $event)
{
if (!$this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return;
}
$url = $this->router->generate('acme_the_route_to_redirect_to');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
使用
parameters:
acme_user.subscriber.fos_user.class:
Acme\UserBundle\EventSubscriber\FOSUserSubscriber
services:
acme_user.subscriber.fos_user:
class: %acme_user.subscriber.fos_user.class%
arguments:
- @router
- @security.context
tags:
- { name: kernel.event_subscriber
这篇关于FOSUserBundle:登录后进行注册重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文