12

我想在注册后将用户重定向到另一个表单,然后他才能访问我网站上的任何内容(例如在https://github.com/FriendsOfSymfony/FOSUserBundle/issues/387中)。

所以我在文档中创建了一个 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;
    }
}

服务.yml:

services:
    rs_user.registration_completed:
        class: rs\UserBundle\EventListener\RegistrationConfirmedListener
        arguments: [@router]
        tags:
            - { name: kernel.event_subscriber }

但它不起作用,用户注册,他点击他邮箱中的确认链接,他没有重定向到我想要的页面,他已经登录,我只有说帐户已确认的消息。

为什么它没有像我想要的那样将我重定向到路线:rsWelcomeBundle_check_full_register?

谢谢

4

5 回答 5

27

要完成您想要的,您应该使用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

于 2013-05-07T20:29:22.347 回答
6
"friendsofsymfony/user-bundle": "2.0.x-dev",

不确定为什么接受的答案对您有用,因为REGISTRATION_CONFIRM在确认令牌后发生。

如果您想执行某个操作,请在 FOS registerAction之后使用其他表单重定向到另一个页面,我建议采用以下方式。

这是一旦提交的表单被 FOS 有效后在registerAction上执行的代码:

FOS\UserBundle\Controller\RegistrationController

        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事件并在发送电子邮件后设置重定向响应。

FOS\UserBundle\EventListener\EmailConfirmationListener

/**
 * @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因为您很可能不想覆盖发送电子邮件的侦听器,因为这是您工作流程的一部分。

简单地:

TB\UserBundle\Controller\RegistrationController

/**
 * @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 同名的文件。)

于 2016-11-20T15:51:20.580 回答
3

也可以使用路由重定向:

fos_user_registration_confirmed:
    path: /register/confirmed
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: redirection_route
        permanent: true
于 2018-12-14T13:04:35.097 回答
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 }
于 2016-04-29T22:15:19.323 回答
0

为了快速解决:您也可以覆盖路线。假设您想重定向到您的主页,您可以执行以下操作:

 /**
 * @Route("/", name="index")
 * @Route("/", name="fos_user_registration_confirmed")
 * @Template(":Default:index.html.twig")
 */
public function indexAction()
{
于 2017-08-05T14:20:04.727 回答