登录后symfony2如何重定向到请求的页面 [英] How does symfony2 redirect to requested page after login

查看:29
本文介绍了登录后symfony2如何重定向到请求的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的路由 /booking/(.*)security.yml 中的防火墙配置保护,它需要ROLE_USER",当用户尝试访问以/booking/"开头的任何路线时,应用会将用户重定向到登录页面进行身份验证.

Say my route /booking/(.*) is protected by a firewall configuration in security.yml and it requires "ROLE_USER", when user tries to access any route that is preceded by "/booking/" the app redirects the user to login page for authentication.

所以我的问题是,在用户提供他的凭据并通过身份验证后,Symfony 2 如何将用户重定向回用户请求的页面/路由或 Symfony 2 在哪里存储该路由 是否将其存储在某个会话或其他地方.

So my question is, after user provides his credentials and gets authenticated, how is Symfony 2 able to redirect the user back to the page/route the user had requested for OR where does Symfony 2 store that route does it store it in some session or some where else.

我们可以访问它吗?如何访问?

推荐答案

这是可能的,您可以访问引用链接(如果 use_referer 设置为 true,则使用该链接>) 在会话中.

This is possible and you can access the referring link (that is used if use_referer is set to true) in the session.

例如,如果您的 form_login(在您的防火墙配置中)上有一个 success_handler 服务,它根据某些标准(通常是角色)重定向用户,但您想将用户重定向到引用链接如果设置了,您可以像这样访问引用链接:

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); 

使用标头获取引用者(即$request->headers->get('referer'))在这种情况下将不起作用,因为它将始终返回登录链接.

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.

感谢 Roman Marintsenko &Ryan Weaver 为这个 博客

Thanks to Roman Marintsenko & Ryan Weaver for this blog

这篇关于登录后symfony2如何重定向到请求的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆