FOSUserBundle : 注册 EventListener 后重定向用户 [英] FOSUserBundle : Redirect the user after register with EventListener
问题描述
我想在用户注册后立即将其重定向到另一个表单,然后他才能访问我网站上的任何内容(例如 https://github.com/FriendsOfSymfony/FOSUserBundle/issues/387).
I want to redirect the user to another form just after registration, before he could access to anything on my website (like in https://github.com/FriendsOfSymfony/FOSUserBundle/issues/387).
所以我在文档中创建了一个 eventListener :
So I create an eventListener like in the doc :
<?php
namespace rsUserBundleEventListener;
use FOSUserBundleFOSUserEvents;
use FOSUserBundleEventUserEvent;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentRoutingGeneratorUrlGeneratorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class RegistrationConfirmedListener implements EventSubscriberInterface
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_CONFIRMED => 'onRegistrationConfirmed'
);
}
public function onRegistrationConfirmed()
{
$url = $this->router->generate('rsWelcomeBundle_check_full_register');
$response = new RedirectResponse($url);
return $response;
}
}
服务.yml:
services:
rs_user.registration_completed:
class: rsUserBundleEventListenerRegistrationConfirmedListener
arguments: [@router]
tags:
- { name: kernel.event_subscriber }
但是没用,用户注册了,他点击邮箱里的确认链接,他没有重定向到我想要的页面,他登录了,我只有消息说帐户已确认.
But it doesn't work, the user register, he click on the confirmation link in his mailbox, he is not redirected on the page I want, he is logged and I just have the message who said the account is confirmed.
为什么它没有像我想要的那样将我重定向到路由:rsWelcomeBundle_check_full_register?
Why it doesn't redirect me to the route : rsWelcomeBundle_check_full_register like I want ?
谢谢
推荐答案
要完成你想要的,你应该使用 FOSUserEvents::REGISTRATION_CONFIRM
而不是 FOSUserEvents::REGISTRATION_CONFIRMED
.
To accomplish what you want, you should use FOSUserEvents::REGISTRATION_CONFIRM
instead of FOSUserEvents::REGISTRATION_CONFIRMED
.
然后你必须重写你的类 RegistrationConfirmedListener
像:
You then have to rewrite rewrite your class RegistrationConfirmedListener
like:
class RegistrationConfirmListener implements EventSubscriberInterface
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_CONFIRM => 'onRegistrationConfirm'
);
}
public function onRegistrationConfirm(GetResponseUserEvent $event)
{
$url = $this->router->generate('rsWelcomeBundle_check_full_register');
$event->setResponse(new RedirectResponse($url));
}
}
还有你的service.yml
:
services:
rs_user.registration_complet:
class: rsUserBundleEventListenerRegistrationConfirmListener
arguments: [@router]
tags:
- { name: kernel.event_subscriber }
REGISTRATION_CONFIRM
接收一个 FOSUserBundleEventGetResponseUserEvent
实例,如您在此处所见:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php
REGISTRATION_CONFIRM
receives a FOSUserBundleEventGetResponseUserEvent
instance as you can see here: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php
它允许您修改将发送的响应:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Event/GetResponseUserEvent.php
It allows you to modify the response that will be sent: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Event/GetResponseUserEvent.php
这篇关于FOSUserBundle : 注册 EventListener 后重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!