登录后如何返回 CakePHP 中的引用页面

Posted

技术标签:

【中文标题】登录后如何返回 CakePHP 中的引用页面【英文标题】:How to go back to referer page in CakePHP after login 【发布时间】:2012-12-19 07:00:25 【问题描述】:

我在返回引用页面时遇到问题:

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

在这个页面我可以编辑里面的东西,所以我有一个login按钮可以进入登录页面,登录成功后我想回到first url

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

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

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

通过刷新页面,并检查users 控制器的login 操作中的凭据,我得到了与以前相同的页面,现在我无法回到first url

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

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

通过使用debug($this->Session->write('redir')); 测试会话变量,一切似乎都很完美,直到我转到login 操作:

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 我得到:

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

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

我想在$this->Auth->login() 操作中使用某种会话保护例程,但这对我来说完全是无稽之谈。

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

【问题讨论】:

那么,您要做的是在用户尝试访问需要身份验证的页面时将其重定向到登录表单,然后在身份验证成功时将其重定向到该页面? 是的,我还没有找到正确的方法。 我猜你知道Auth 组件内置了这个功能。只需在if ($this->Auth->login()) 块中执行$this->redirect($this->Auth->redirect());。你不这样做有什么原因吗? 是的,这是我的第一次尝试,但是我遇到了登录问题,一旦第二次调用操作登录来检查用户凭据,引用者就是视图登录而不是之前的登录。我确定我遗漏了一些明显的东西,但我不知道它是什么。 @VittorioVittori 我明白你在说什么,但Auth 组件考虑到了这一点。看看the code 自己看看。顺便说一句,您的代码不应该尝试通过 CakePHP 访问 javascript 文件。直接访问浏览器中的 JavaScript 文件(例如 example.com/js/jquery/plugin/jquery.validata.1.7.min.js )是否有效?您使用的是 Apache 并且启用了“mod_rewrite”吗? 【参考方案1】:

你试过这个:$this->redirect(Controller::referer());

我必须重新阅读您的整个问题才能确定,但​​这也应该有效:$this->redirect( $this->referer() );

【讨论】:

【参考方案2】:

我使用会话来保持上次访问的 url,如果登录成功,用户将被重定向到它。

首先在 AppController.php 中,我将 url 保存在会话中,登录和注销操作除外:

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

在用于登录操作的 UserController.php 中,我有以下代码:

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

【讨论】:

【参考方案3】:

我就是这样做的:

public function login() 
    if ($this->request->is('post')) 
        if ($this->Auth->login()) 
            $this->redirect($this->Session->read('referer'));
         else 
            $this->Session->setFlash(__('Não foi possível efetuar o login. Usuário ou senha inválidos'), 'default', array('class' => 'alert alert-danger'));
        
     else 
         $this->Session->write('referer', $this->referer());
    

【讨论】:

以上是关于登录后如何返回 CakePHP 中的引用页面的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP 登录后重定向到当前页面

我可以从 cakephp 中的视图重定向吗

如果管理员在cakephp3.x中将其停用,如何限制db中的数据保存?

Cakephp3:如何在询问标记后使用get参数获取当前页面的完整URL

django:登录后重定向到引用页面[重复]

Cakephp:选择登录后要保存的数据