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
Best Solution
To accomplish what you want, you should use
FOSUserEvents::REGISTRATION_CONFIRM
instead ofFOSUserEvents::REGISTRATION_CONFIRMED
.You then have to rewrite rewrite your class
RegistrationConfirmedListener
like:And your
service.yml
:REGISTRATION_CONFIRM
receives aFOS\UserBundle\Event\GetResponseUserEvent
instance as you can see here: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/FOSUserEvents.phpIt allows you to modify the response that will be sent: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Event/GetResponseUserEvent.php