CakePHP auth组件不登录用户

时间:2013-03-08 10:45:54

标签: php cakephp cakephp-2.3

这是我的AppController.php

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array(
                'controller' => 'students',
                'action' => 'login'
            ),
            'loginRedirect' => array('controller' => 'students', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'students', 'action' => 'login'),
            'authError' => "You can't acces that page",
            'authorize' => array('controller')
        )
    );

    public function isAuthorized($student){
        return TRUE;
    }
}

这是我的StudentsController.php

class StudentsController extends AppController{


        public function login(){
            if($this->request->is('post')){
                if($this->Auth->login()){
                    $this->redirect($this->Auth->redirect());
                }else{
                    $this->Session->setFlash('Your username or password is incorrect');
                }
            }
        }

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

这是我的login.ctp

<h2>Login</h2>
<?php
    echo $this->Form->create();
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Login');
?>

在我的数据库中,表名为学生,其字段为用户名密码

但每次我尝试使用$this->Auth->login()登录时,都会返回false和密码错误的Flash消息显示,我无法登录。

1 个答案:

答案 0 :(得分:3)

由于您使用的是Users以外的其他模型进行身份验证,因此您需要将该模型名称提供给AuthComponent

$this->Auth->authenticate = array(
    AuthComponent::ALL => array('userModel' => 'Student'),
    'Form',
    'Basic'
);

您应该阅读documentation