使用最新的Symfony和FOSUserbundle,在成功注册新用户之后,用户将自动登录。我想阻止这一切。我的理由是,只有特殊用户才能注册新用户。
我想我必须在包的registerAction中重写RegisterController,但我不知道如何实现。
我试过:controllers.html,但是它似乎已经过时了,没有任何用户是用这个方法创建的。
如有任何提示,将不胜感激。
编辑:
我发现我没有正确地创建子包。我还必须创建自己的EventListener。当我覆盖FOSUserEvents::REGISTRATION_SUCCESS
事件时,它现在起作用了。
奇怪的是,当我使用FOSUserEvents::REGISTRATION_COMPLETED
事件时,两个事件都会被分派,我的包的事件和FOSUserbundle的事件,这样用户就被重定向到正确的站点,但作为新用户登录。
编辑2:
这是我的听众说的:
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
是空的。通过调试器,我可以验证是否调用了我自己的侦听器事件,但是也调用了原始事件。
发布于 2017-01-04 21:41:34
您就快到了,正如您所说的,您的侦听器被调用了,但是顺序不正确,因此您需要在默认的监听器之前执行监听器,以完成该更改。
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess‘
至
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',-10,
注意这里的-10,这会改变侦听器的优先级。
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
的输出
https://stackoverflow.com/questions/34926573
复制相似问题