FOSUserBundle : Redirect the user after register with EventListener

后端 未结 5 1230
有刺的猬
有刺的猬 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 09:08

    If you're not using a confirmation email, you can redirect the user right after submiting the registration form this way :

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

    The subscriber declaration stay the same :

    registration_confirmation_subscriber:
        class: AppBundle\Subscriber\RegistrationConfirmationSubscriber
        arguments:
            - "@router"
        tags:
            - { name: kernel.event_subscriber }
    

提交回复
热议问题