如何在登录后返回到CakePHP中的查阅器页面 [英] How to go back to referer page in CakePHP after login

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

问题描述

回到查阅网页时出现问题:

 第一个网址:http://site.com/venue / apartments / 1564 / venue-name 
referer url:null

编辑内部的东西,所以我有一个登录按钮去登录页面,成功登录后,我想回到第一url

 第二个网址:http://site.com/users/login 
referer url:http://site.com/venue/apartments/1564/venue-name

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

 第三个网址:http://site.com/users/login 
referer url:http://site.com/users/login

通过刷新页面并检查登录 用户的操作控制器我得到作为引用与我之前的相同的页面,现在我不能回到第一个URL



我尝试了一些解决方案,通过在AppController中设置一个会话变量来检查我是否不在登录操作页面并执行此操作:

  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与 debug($ this-> Session-> write('redir')); 一切似乎工作完美,直到我去登录 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());
}
}
}
}

这打破了应用程序,如果我 debug($ redir); var i get:

  array(
'controller'=>'js',
'action'=>'jquery',
(int)0 =>'plugin / jquery。 validata.1.7.min'//只是一个jQuery插件加载在default.ctp

而不是

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

如果我在整个网站的任何其他视图中的$ debug($ redir); ,一切正常,我得到正确的数据。 / p>

我想在某种类型的会话保护例程 $ this-> Auth-> login()



我如何重定向登录到最后一页的用户不是登录查看页面?

解决方案

您尝试过: $ this-& :: referer());



我必须重读你的整个问题才能确定,但​​这也应该工作: code> $ this-> redirect($ this-> referer());


I have a problem on going back to referer page:

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

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

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

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.

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'], '/')));
    }
}

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

Doing this break the application and if I debug($redir); var i get:

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

instead of

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.

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?

解决方案

Have you tried this: $this->redirect(Controller::referer());?

I'd have to reread your whole question to be sure, but this should also work: $this->redirect( $this->referer() );

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

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