I am building a web application with Symfony 2, using the FOSUserBundle bundle.
Users create an account, login and start using the application.
What I want to achieve now is to have the user redirected to their account from any page they may be at if they are logged in.
This includes:
- if they get back to the login page
- if they get back to the registration page
- if they go to the homepage of the website
- once they confirm their email
- once they reset their password
Basically the code would be something like this:
$container = $this->container;
$accountRouteName = "DanyukiWebappBundle_account";
if( $container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
// authenticated (NON anonymous)
$routeName = $container->get('request')->get('_route');
if ($routeName != $accountRouteName) {
return $this->redirect($this->generateUrl($accountRouteName));
}
}
The problem is I don’t know where that code should go.
It should be executed for any request. In Symfony 1 I would have used a filter.
I found the solution myself:
And then, in the services.yml file of my bundle: