CakePHP Auth组件重定向问题

Ale*_*ian 5 php authentication model-view-controller redirect cakephp-1.2

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

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

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

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

    /* [...] */
}
Run Code Online (Sandbox Code Playgroud)

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

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

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('*');
      }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 11

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

function beforeFilter() {
    $this->Auth->autoRedirect = false;
    parent::beforeFilter();
}
Run Code Online (Sandbox Code Playgroud)

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

$this->redirect($this->Auth->redirect());
Run Code Online (Sandbox Code Playgroud)

Auth-> redirect()返回用户在进入登录页面或Auth-> loginRedirect之前登陆的URL.