CakePHP Auth 组件重定向问题

时间:2023-01-04
本文介绍了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 组件重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP 可以与 XSLT 通信吗? 下一篇:学习 Zend 框架的最快方法?

相关文章

最新文章