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
Route redirection can also be used:
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
Not sure why the accepted answer works for you as REGISTRATION_CONFIRM happens after the token is confirmed.
In case you want to perform an action, redirect to another page with some additional form after the FOS registerAction I would suggest the following way.
This is the code that is performed on registerAction once the submitted form is valid by FOS:
FOS\UserBundle\Controller\RegistrationController
As you can see the first possible return happens after FOSUserEvents::REGISTRATION_SUCCESS event in case the response is null which in my case doesn't as I have configured a mailer to send a confirmation token and FOS is using an listener that listens to this FOSUserEvents::REGISTRATION_SUCCESS event and after sending an email it sets a redirect response.
FOS\UserBundle\EventListener\EmailConfirmationListener
Okay I understand! So how do I redirect to another page?
I would suggest to overwrite checkEmailAction as most likely you don't want to overwrite the listener that sends an email as that's part of your workflow.
Simply:
TB\UserBundle\Controller\RegistrationController
As you can see instead of rendering FOS's check_email template I decided to redirect user to his new profile.
Docs how to overwrite an controller: https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_controllers.html (basically define a parent for your bundle and create a file in the directory with the same name as FOS does.)
If you're not using a confirmation email, you can redirect the user right after submiting the registration form this way :
The subscriber declaration stay the same :
For a quick solution: you can also override the route. Let's say you want to redirect to your homepage you can do something like this: