我想在注册之后将用户重定向到另一个表单,然后才能访问我网站上的任何内容(例如https://github.com/FriendsOfSymfony/FOSUserBundle/issues/387)。
所以我在doc:
中创建了一个eventListener<?php
namespace rs\UserBundle\EventListener;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\UserEvent;
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 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;
}
}
Services.yml:
services:
rs_user.registration_completed:
class: rs\UserBundle\EventListener\RegistrationConfirmedListener
arguments: [@router]
tags:
- { name: kernel.event_subscriber }
但它不起作用,用户注册,他点击他邮箱中的确认链接,他没有被重定向到我想要的页面,他被记录了,我只是有消息谁说帐户已经确认。
为什么它不会将我重定向到路线:rsWelcomeBundle_check_full_register就像我想要的那样?
由于
答案 0 :(得分:26)
要完成您想要的操作,您应该使用FOSUserEvents::REGISTRATION_CONFIRM
代替FOSUserEvents::REGISTRATION_CONFIRMED
。
然后你必须重写你的课程RegistrationConfirmedListener
,如:
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: rs\UserBundle\EventListener\RegistrationConfirmListener
arguments: [@router]
tags:
- { name: kernel.event_subscriber }
REGISTRATION_CONFIRM
收到FOS\UserBundle\Event\GetResponseUserEvent
个实例,您可以在此处看到:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php
它允许您修改将要发送的响应:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Event/GetResponseUserEvent.php
答案 1 :(得分:6)
"friendsofsymfony/user-bundle": "2.0.x-dev",
不确定为什么接受的答案适合您,因为 REGISTRATION_CONFIRM 在确认令牌后发生。
如果你想要执行某个操作,请在FOS registerAction 后重定向到另一个带有其他格式的页面。我建议采用以下方法。
这是在FOS提交的表单有效后,在 registerAction 上执行的代码:
if ($form->isValid()) {
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
正如您所看到的那样,第一个可能的返回发生在 FOSUserEvents :: REGISTRATION_SUCCESS 事件之后,如果响应为空,在我的情况下没有,因为我已经配置了邮件程序发送确认令牌,FOS正在使用侦听此 FOSUserEvents :: REGISTRATION_SUCCESS 事件的侦听器,并在发送电子邮件后设置重定向响应。
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
);
}
/**
* @param FormEvent $event
*/
public function onRegistrationSuccess(FormEvent $event)
{
/** @var $user \FOS\UserBundle\Model\UserInterface */
$user = $event->getForm()->getData();
$user->setEnabled(false);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$this->mailer->sendConfirmationEmailMessage($user);
$this->session->set('fos_user_send_confirmation_email/email', $user->getEmail());
$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));
}
我建议覆盖 checkEmailAction ,因为很可能您不想覆盖发送电子邮件的侦听器,因为这是您工作流程的一部分。
简单地:
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function checkEmailAction()
{
/** @var UserManager $userManager */
$userManager = $this->get('fos_user.user_manager');
/** @var string $email */
$email = $this->get('session')->get('fos_user_send_confirmation_email/email');
$user = $userManager->findUserByEmail($email);
return $this->redirect($this->generateUrl('wall', ['username' => $user->getUsername()]));
}
正如您所看到的,我决定将用户重定向到他的新个人资料,而不是渲染FOS的 check_email 模板。
文档如何覆盖控制器:https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_controllers.html(基本上为您的包定义父级,并在目录中创建一个与FOS同名的文件。)
答案 2 :(得分:2)
如果您未使用确认电子邮件,则可以在以这种方式提交注册表后立即重定向用户:
class RegistrationConfirmationSubscriber implements EventSubscriberInterface
{
/** @var Router */
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationConfirm'];
}
public function onRegistrationConfirm(FilterUserResponseEvent $event)
{
/** @var RedirectResponse $response */
$response = $event->getResponse();
$response->setTargetUrl($this->router->generate('home_route'));
}
}
订阅者声明保持不变:
registration_confirmation_subscriber:
class: AppBundle\Subscriber\RegistrationConfirmationSubscriber
arguments:
- "@router"
tags:
- { name: kernel.event_subscriber }
答案 3 :(得分:2)
还可以使用路由重定向:
fos_user_registration_confirmed:
path: /register/confirmed
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: redirection_route
permanent: true
答案 4 :(得分:0)
快速解决方案:您也可以覆盖路线。假设你想重定向到你的主页,你可以这样做:
/**
* @Route("/", name="index")
* @Route("/", name="fos_user_registration_confirmed")
* @Template(":Default:index.html.twig")
*/
public function indexAction()
{