CakePHP Auth 组件重定向问题

CakePHP Auth component redirect issue(CakePHP Auth 组件重定向问题)
本文介绍了CakePHP Auth 组件重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我无法让 Auth 组件在 CakePHP 1.2.6 应用程序中执行我想要的重定向.

I am having trouble getting the Auth component do the redirects I want in a CakePHP 1.2.6 app.

我有一个显示在所有页面上的登录表单,我想将用户保留在他登录的页面上.例如,如果他正在查看另一个用户的个人资料,我想在登录后将他留在那里,而不是将他重定向到 $this->Auth->loginRedirect 操作.此外,关于我的应用程序的另一件事是我没有仅限经过身份验证的访问"页面,每个人都可以访问每个页面,但如果您登录,您可以获得其他功能.

I have a login form that appears on all pages and I want to keep the user on the page he logs in on. For example, if he is viewing another user's profile, I want to keep him there after logging in, not redirect him to the $this->Auth->loginRedirect action. Also, another thing about my app is that I have no "authenticated access only" pages, every page is accessible to everyone, but if you're logged in you get additional features.

我从阅读文档中了解到我需要设置autoRedirect 为 false 以获取要执行的 login() 函数中的代码:

What I understood from reading the documentation is that I need to set autoRedirect to false to get the code in the login() function to be executed:

class UsersController extends AppController {    
    var $name = 'Users';
    var $helpers = array('Html', 'Form','Text');

    function beforeFilter() {
        $this->Auth->autoRedirect = false;
    }

    function login() {
        $this->redirect($this->referer());
    }

    function logout() {
        $this->redirect($this->Auth->logout());
    }

    /* [...] */
}

这目前破坏了我的身份验证.我注意到(从日志中)如果我在登录功能中保留重定向并将 autoRedirect 设置为 false,则 $this->data 中的密码字段login() 函数显示为空.

This currently breaks my authentication. I've noticed (from the logs) that if I leave the redirect in the login function and set autoRedirect to false, the password field in $this->data in the login() function appears as empty.

下面,我已经发布了与 Auth 组件相关的 AppController 的内容:

Below, I've posted the contents of AppController that relate to the Auth component:

public function beforeFilter() {

    $this->Auth->fields = array(
        'username' => 'email',             
        'password' => 'password'            
    );

    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');     
    $this->Auth->loginRedirect = array('controller' => 'usercars', 'action' => 'homepage');

    $this->allowAccess();

    // build wishlist if the user is logged in
    if ($currentUser = $this->Auth->user()) {
        $wishlists = $this->buildWishlist($currentUser);
        $this->set('wishlists', $wishlists);
    }

}

private function allowAccess() {
      if(in_array($this->name, /* all my controller names */)) {
          $this->Auth->allow('*');
      }
}

我似乎无法理解我做错了什么.

I can't seem to understand what I'm doing wrong.

推荐答案

Add parent::beforeFilter();到用户控制器中的 beforeFilter:

Add parent::beforeFilter(); to beforeFilter in the user controller:

function beforeFilter() {
    $this->Auth->autoRedirect = false;
    parent::beforeFilter();
}

您还可以将重定向替换为您的用户控制器的登录方法:

You can also replace the redirect with this to the login method of your user controller:

$this->redirect($this->Auth->redirect());

Auth->redirect() 返回用户在被带到登录页面或 Auth->loginRedirect 之前登陆的 url.

Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.

这篇关于CakePHP Auth 组件重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)