Symfony2:注册后检查电子邮件
我第一次在这个论坛上发帖子,我经常使用这个论坛。我在Symfony2应用程序中使用FOSUserbundle来管理用户。当用户通过以下方式创建帐户时,我激活了发送电子邮件确认:Symfony2:注册后检查电子邮件,symfony,fosuserbundle,Symfony,Fosuserbundle,我第一次在这个论坛上发帖子,我经常使用这个论坛。我在Symfony2应用程序中使用FOSUserbundle来管理用户。当用户通过以下方式创建帐户时,我激活了发送电子邮件确认: fos_user: registration: confirmation: enabled: true 它工作得很好:电子邮件发送成功。我被重定向到页面/检查电子邮件,该页面显示此电子邮件已发送。但是,我希望更改重定向:我希望被重定向到我的索引页,而不是查看/检查电
fos_user:
registration:
confirmation:
enabled: true
它工作得很好:电子邮件发送成功。我被重定向到页面/检查电子邮件,该页面显示此电子邮件已发送。但是,我希望更改重定向:我希望被重定向到我的索引页,而不是查看/检查电子邮件。所以我做了研究,我知道他必须经历FOSUserBundle事件()
我所做的:
class RegistrationListener implements EventSubscriberInterface {
private $router;
public function __construct(UrlGeneratorInterface $router) {
$this->router = $router;
}
public static function getSubscribedEvents() {
return array(
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm');
}
public function onRegistrationConfirm(FormEvent $event) {
$url = $this->router->generate('listeArticlesAccueil');
$event->setResponse(new RedirectResponse($url));
}
}
和服务.yml
services:
listener_user.registration_listener:
class: My\Soft\UserBundle\EventListener\RegistrationListener
arguments: [@router]
tags:
- { name: kernel.event_subscriber }
问题是每次我被重定向到页面/检查电子邮件时。因此,我告诉我,这可能是错误的事件。所以我也试着成功了。但一切都没有改变。所以要么我没有使用正确的事件,要么我做错了什么
不管是哪种情况,我希望你能帮助我
非常感谢,很抱歉我的英语不好;) 我敢打赌注册成功。 医生说 此事件允许设置响应。
注册确认只允许访问用户,而不允许使用
注册确认
以下是事件侦听器的完整代码:
/**
* Description of RegisterationConfirmListener
*
* @author imran
*/
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Description of RegisterationConfirmListener
*
* @author imran
*/
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class RegisterationConfirmListener implements EventSubscriberInterface {
private $router;
public function __construct(UrlGeneratorInterface $router) {
$this->router = $router;
}
public static function getSubscribedEvents() {
return [
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegisterationConfirm',
];
}
public function onRegisterationConfirm(GetResponseUserEvent $event) {
$url = $this->router->generate('home');
$event->setResponse(new RedirectResponse($url));
}
}
对于服务.yml
:
app_user.registeration_confirmed:
class: Wishlocker\AppBundle\EventListener\RegisterationConfirmListener
arguments: [ @router ]
tags:
- { name: kernel.event_subscriber }
我知道这个问题很久以前就被贴出来了,但是我找到了一个解决方案,我想把它分享给其他有同样问题的人 在这种情况下,您应该使用的事件是注册成功 在FOS\UserBundle\EventListener\EmailConfirmationListener::onRegistrationSuccess之前,必须对要调用的侦听器注册\u成功进行优先级排序,如下所示:
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS => [
['onRegistrationSuccess', -10],
],
];
}
如果您不这样做,EmailConfirmationListener将被提前调用,您将被重定向到fos\u用户\u注册\u检查\u电子邮件路由
以下是我所做的:
class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onRegistrationSuccess(FormEvent $event)
{
$event->stopPropagation();
$url = $this->router->generate('homepage');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS => ['onRegistrationSuccess',-10],
];
}
}
和in-app/services.yml:
app.redirect_after_registration_subscriber:
class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
arguments: ['@router']
tags:
- { name: kernel.event_subscriber }
我希望这有帮助。你说得对!我试过了,成功了。当我没有激活电子邮件确认时,我将正确重定向到我的页面。但是当我激活确认时(使用config.yml文件),我会被重定向到/检查电子邮件。。。我不知道为什么!