CakePHP登录重定向到请求的URL [英] CakePHP log in redirect to requested URL

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

问题描述

当用户点击需要登录的链接时,我们当前将其重定向到登录页面,但是我们丢失了所需的网址。

When a user clicks a link that requires a log in, we currently redirect them to the log-in page ,but we lose the intended URL. What is the best way to account for that URL and redirect the user to the requested page once logged in?

我们使用的是最新的稳定版本的Cake,这是什么是最好的方式来解释该网址并将用户重定向到请求的页面?非常感谢。

We're using the latest stable version of Cake. Thanks.

- 编辑

p>

Its configured like this

$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
// $this->Auth->autoRedirect = false;
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');

在users_controller中的登录函数

login function in users_controller

function login() {
    Debugger::log("Redirect Session Key:" . $this->Session->read('Auth.redirect'), $level = 7);
    $ref = $this->referer();
    Debugger::log("referer():" . $ref, $level = 7);
}

当我导航到未授权的页面,如localhost / mysite /未授权我重定向到localhost / mysite / users / login。在我的debug.log文件重定向会话密钥打印为用户/仪表板,这意味着没有有效的引用者。为了验证这个假设,我还打印出登录函数中的引用者,它实际上返回/,这是在我相信请求者没有引用者信息的情况下的默认值。

When I navigate to an unauthorized page like localhost/mysite/unauthorized I am redirected to localhost/mysite/users/login. In my debug.log file The Redirect Session Key prints out as users/dashboard which implies that there is no valid referrer. To validate that assumption I also print out the referrer in the login function which does in fact return "/" which is the default value in the case of no referrer information from the requester I believe.


根据蛋糕文档,如果用户不是来自外部链接,authcomponent只保留这个在会话中。

p>

阅读部分


AuthComponent会记住您尝试获取的控制器/操作对在您被要求验证自己之前,将此值存储在会话中的 Auth.redirect 键下。但是,如果未设置此会话值(例如,如果您通过外部链接进入登录页面),则用户将被重定向到loginRedirect中指定的网址。

The AuthComponent remembers what controller/action pair you were trying to get to before you were asked to authenticate yourself by storing this value in the Session, under the Auth.redirect key. However, if this session value is not set (if you're coming to the login page from an external link, for example), then the user will be redirected to the URL specified in loginRedirect.

它会记住您尝试访问的页面,除非您直接转到登录页面 ,因此没有您来自的上一页。如果链接到 / foo / secret_page ,并且重定向到登录页面,应该记住 / foo / secret_page

It will remember the page you were trying to access, unless you went to the login page directly and hence there's no previous page you came from. If you link to /foo/secret_page and are redirected to the login page though, /foo/secret_page should be remembered in the session.

如果你摆脱了 $ this-> Auth-> autoRedirect = false

否则你可以读取并设置'Auth.redirect'

Otherwise you can read and set the 'Auth.redirect' session key manually to have more control over redirects.

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

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