如何回去引用者页面的CakePHP登录后 [英] How to go back to referer page in CakePHP after login

查看:166
本文介绍了如何回去引用者页面的CakePHP登录后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有回到引用者页面的一个问题:

I have a problem on going back to referer page:

first url: http://site.com/venue/apartments/1564/venue-name
referer url: null

在此页我可以在里面编辑的东西,所以我有一个登录按钮即可进入登录页面,成功登录后,我想回头在第一个URL

In this page I can edit stuff inside, so I have a login button to go to the login page, after a successful login, I would like to turn back to the first url.

second url: http://site.com/users/login
referer url: http://site.com/venue/apartments/1564/venue-name

当我尝试登录,由于CakePHP的操作规则,我必须刷新登录页面,检查凭据时,问题从这里开始:

when I try to login, due to the CakePHP action rules, i have to refresh the login page to check the credentials, the problem starts here:

third url: http://site.com/users/login
referer url: http://site.com/users/login

刷新页面,并检查在登录凭据 用户的动作控制器我得到的转介同样的网页,我是以前,现在我不能要回第一个URL

by refreshing the page, and checking the credentials in the login action of users controller I get as refer the same page where I was before, and now I can't going back to the first url.

我尝试了一些解决方案,通过设置,如果我在登录的操作页面可不是它检查AppController的会话变量,做这样的:

I tried some solution by setting a session variable in the AppController which checks if I'm not in the login action page and doing this:

AppController.php

public function beforeFilter () {
    $this->setRedirect();
}

public function setRedirect () {
    if ($this->request->params['controller'] != 'users' && $this->request->params['action'] != 'login') {
        $this->Session->write('redir', array('controller'=>$this->request->params['controller'], 'action'=>$this->request->params['action'], implode($this->request->params['pass'], '/')));
    }
}

通过与调试测试会话VAR($这个 - >会话级>写(再导向')); 一切似乎完美的作品,直到我去在登录动作:

By testing the session var with debug($this->Session->write('redir')); everything would seem to works perfect until I go to the login action:

UsersController.php

public function login () {
    if ($this->request->is ('post')){
        if ($this->Auth->login()) {
            $redir = $this->Session->read('redir');
            if (!empty($redir)) {
                $this->redirect ($redir);
            } else {
                $this->redirect ($this->Auth->redirect());
            }
        }
    }
}

这样做破坏应用程序,如果我调试($ REDIR); VAR我得到:

array(
    'controller' => 'js',
    'action' => 'jquery',
    (int) 0 => 'plugin/jquery.validata.1.7.min' // just a jquery plugin loaded in default.ctp
)

而不是

array(
    'controller' => 'venue',
    'action' => 'apartments',
    (int) 0 => '1564/venue-name'
)

如果我调试($再导向); 整个网站的任何其他看法,一切正常,我也得到正确的数据。

If I debug($redir); in any other view of the entire site, everything works well and I get the right data.

我想在某种会话保护程序,为 $这个 - > Auth-方式>登录()行动,但它是完全胡说八道,我

I thought in some kind of Session protection routine for $this->Auth->login() action but it's totally nonsense for me.

我如何才能重定向记录到最后一页用户这不是登录视图页面?

How can I just redirect the user logged to the last page which isn't the login view page?

推荐答案

我使用的会话保持最后访问的URL,如果登录成功,用户将被重定向到它。

I used sessions to keep the last url accessed and if the login is successful the user will be redirected to it.

先在AppController.php我在会议上保存的URL,除了登录和注销行为:

First in AppController.php I save in session the url, except for login and logout actions:

public function beforeFilter() {
    $url = Router::url(NULL, true); //complete url
    if (!preg_match('/login|logout/i', $url)){
        $this->Session->write('lastUrl', $url);
    }
}

在UserController.php进行登录操作,我有以下code:

In the UserController.php for login action I have the following code:

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            if ($this->Session->read('lastUrl')){
                $this->redirect($this->Session->read('lastUrl'));
                exit;
            }else{
                return $this->redirect($this->Auth->redirect());
            }
        }
        $this->Session->setFlash(__('Invalid username or password'));
    }
}

这篇关于如何回去引用者页面的CakePHP登录后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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