I'm trying to redirect if a user is not logged in (works with FOSUser and SonataAdmin) and calls either http://domain.com/app_dev.php
(dev) or http://domain.com
(prod) then I want in both cases redirect to /login
so I wrote this config at app/routing.yml
:
root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /login
permanent: true
That works but I have a problem since I have this in a controller:
/**
* @param Request $request
*
* @return array
* @Route("/", name="brand")
* @Method("GET")
* @Template()
*/
public function indexAction(Request $request)
{
....
}
Then when I try to call brand
route as per example using a link like: http://domain.com/app_dev.php/?email=7x-xRFClkjw
(dev) or http://domain.com/?email=7x-xRFClkjw
(prod) I got redirected to /login
. My best idea is to change my code into this:
/**
* @param Request $request
*
* @return array
* @Route("/bp", name="brand")
* @Method("GET")
* @Template()
*/
public function indexAction(Request $request)
{
....
}
And then redirect using a route to the new /bp
instead to /
for that function (this is mainly because users already has a tons of emails that had links as the previously shared and I can't change that). But I've not idea in how to write this rule in routing.yml
because if I wrote this:
bp:
path: /
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: brand
permanent: true
I will end up with a redirect loop. I could change also how default route to /login
is setup I just can't find the right documentation. The big idea behind this post is to setup default route for /
. Can any give me some help on this?
As extra info this is part of my security.yml
file:
firewalls:
...
admin_area:
pattern: ^/
anonymous: ~
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
# the user is redirected here when they need to log in
login_path: /login
# submit the login form here
check_path: /login_check
# login success redirecting options (read further below)
always_use_default_target_path: true
default_target_path: /admin/dashboard
target_path_parameter: _target_path
use_referer: false
failure_path: /admin/dashboard
failure_forward: false
logout:
path: /logout
UPDATE
Actually the code from @emmanuel-hdz-díaz give me another idea and I don't need to create a kernel listener or add to much code, simple by doing this at my controller:
if ($request->query->get('email')) {
...
} else {
return $this->redirect($this->generateUrl('sonata_user_admin_security_login'));
}
I was able to redirect the user to the /login
route.