首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Symfony的FOSUserbundle中注册后禁用自动登录

在Symfony的FOSUserbundle中注册后禁用自动登录
EN

Stack Overflow用户
提问于 2016-01-21 14:35:18
回答 4查看 4.2K关注 0票数 6

使用最新的Symfony和FOSUserbundle,在成功注册新用户之后,用户将自动登录。我想阻止这一切。我的理由是,只有特殊用户才能注册新用户。

我想我必须在包的registerAction中重写RegisterController,但我不知道如何实现。

我试过:controllers.html,但是它似乎已经过时了,没有任何用户是用这个方法创建的。

如有任何提示,将不胜感激。

编辑:

我发现我没有正确地创建子包。我还必须创建自己的EventListener。当我覆盖FOSUserEvents::REGISTRATION_SUCCESS事件时,它现在起作用了。

奇怪的是,当我使用FOSUserEvents::REGISTRATION_COMPLETED事件时,两个事件都会被分派,我的包的事件和FOSUserbundle的事件,这样用户就被重定向到正确的站点,但作为新用户登录。

编辑2:

这是我的听众说的:

代码语言:javascript
运行
复制
public static function getSubscribedEvents()
{
    return array(
        FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted',
    );
}

public function onRegistrationSuccess(FormEvent  $event)
{
    $url = $this->router->generate('admin');

    $event->setResponse(new RedirectResponse($url));
}

public function onRegistrationCompleted(FilterUserResponseEvent  $event)
{

}

我在REGISTRATION_SUCCESS事件中设置了重定向,REGISTRATION_COMPLETED是空的。通过调试器,我可以验证是否调用了我自己的侦听器事件,但是也调用了原始事件。

EN

Stack Overflow用户

发布于 2017-01-04 21:41:34

您就快到了,正如您所说的,您的侦听器被调用了,但是顺序不正确,因此您需要在默认的监听器之前执行监听器,以完成该更改。

FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess‘

FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',-10,

注意这里的-10,这会改变侦听器的优先级。

代码语言:javascript
运行
复制
class RegistrationSuccessEventListener implements EventSubscriberInterface{


private $router;

public  function __construct(UrlGeneratorInterface $router){

        $this->router = $router;

}

public static function getSubscribedEvents()
{
    //this will be called before

    return array(
        FOSUserEvents::REGISTRATION_SUCCESS => ['onUserRegistrationSuccess', -30],
    );

}

/**
 * @param FormEvent $event
 * When the user registration is completed redirect
 * to the employee list page and avoid the automatic
 * mail sending and user authentication that happends
 *
 */
public  function  onUserRegistrationSuccess(FormEvent $event){



    $url = $this->router->generate('employees_list');

    $event->setResponse(new RedirectResponse($url));

}




}

我在FOSBundle版本中使用Symfony2.8

friendsofsymfony/ FOSUserBundle dev-master 1f97ccf Symfony FOSUserBundle

根据composer info的输出

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34926573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档