This is possible and you can access the referring link (that is used if use_referer
is set to true
) in the session.
For instance, if you have a success_handler
service on your form_login
(in your firewall configuration) that redirects users based on some criteria (commonly roles) but you wanted to redirect the user to the referrer link if it was set you could access the referrer link like so:
$key = '_security.main.target_path'; #where "main" is your firewall name
//check if the referrer session key has been set
if ($this->container->get('session')->has($key)) {
//set the url based on the link they were trying to access before being authenticated
$url = $this->container->get('session')->get($key);
//remove the session key
$this->container->get('session')->remove($key);
}
//if the referrer key was never set, redirect to a default route
else{
$url = $this->router->generate('member_home');
}
return new RedirectResponse($url);
Using the header to get the referrer (ie $request->headers->get('referer')
) will not work in this case because it will always return the login link.
Thanks to Roman Marintsenko & Ryan Weaver for this blog
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…