Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSUserBundle : Redirect the user after register with EventListener

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/FOSUserBundle/issues/387).

So I create an eventListener like in the doc :

<?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 }

But it doesn't work, the user register, he click on the confirmation link in his mailbox, he is not redirected on the page I want, he is logged and I just have the message who said the account is confirmed.

Why it doesn't redirect me to the route : rsWelcomeBundle_check_full_register like I want ?

Thanks

like image 297
user2178964 Avatar asked May 07 '13 19:05

user2178964


2 Answers

Route redirection can also be used:

fos_user_registration_confirmed:
    path: /register/confirmed
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: redirection_route
        permanent: true
like image 136
jo66 Avatar answered Oct 30 '22 17:10

jo66


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

like image 42
cheesemacfly Avatar answered Oct 30 '22 18:10

cheesemacfly