且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

FOSUserBundle:向EventListener注册后重定向用户

更新时间:2023-09-28 22:57:58

要完成所需的操作,应使用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: 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

REGISTRATION_CONFIRM receives a FOS\UserBundle\Event\GetResponseUserEvent 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