FOSUserBundle : Redirect the user after register with EventListener

后端 未结 5 1233
有刺的猬
有刺的猬 2020-12-03 08:07

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/FOSUserBun

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 08:48

    To accomplish what you want, you should use FOSUserEvents::REGISTRATION_CONFIRM instead of FOSUserEvents::REGISTRATION_CONFIRMED.

    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));
        }
    }
    

    And your service.yml:

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

    REGISTRATION_CONFIRM receives a FOS\UserBundle\Event\GetResponseUserEvent instance as you can see here: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.php

    It allows you to modify the response that will be sent: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Event/GetResponseUserEvent.php

提交回复
热议问题