0

这是我的 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;
    }
}

这是我的 StudentController.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 消息,我无法登录。

4

1 回答 1

3

因为您使用的是其他模型而不是Users用于身份验证,所以您需要将该模型名称提供给AuthComponent.

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

你应该阅读文档

于 2013-03-08T11:00:12.800 回答