3

我有一个简单的登录表单,就像蛋糕博客教程一样。当我使用“UsersController”和“User”模型命名约定,在调试中传递权限查询时,它就像一个魅力。

但是当我将其更改为其他名称时,在我的情况下为 Alunos,它不会生成任何查询并给我“不正确的用户名和/或密码。”。

我的登录.ctp

<H1> Login </H1>

<?php
    debug($this->data);
    echo $this->Form->create('Aluno', array('action' => 'login'));
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Login');
?>

我的应用控制器

<?php

class AppController extends Controller {

    public $components = array (
        'Session',
        'Auth' => array (
            'loginAction' => array ('controller'=>'alunos', 'action'=>'login'),
            'loginRedirect'=>array ('controller'=>'alunos', 'action'=>'inicio'),
            'logoutRedirect'=>array ('controller'=>'alunos', 'action'=>'index'),
            'authError'=>"Ops, você não está autorizado a fazer isso.",
            'authorize'=>array('Controller'),
        )
    );

    public function isAuthorized($user) {
        return true;
    }

    public function beforeFilter() {
        $this->Auth->allow('index', 'add');
        $this->set('logged_in', $this->Auth->loggedIn());
        $this->set('current_user', $this->Auth->user());

    }

}

还有我的“AlunosController.php”(看到它不是USERSController,就像普通代码一样)

<?php

class AlunosController extends AppController {  

    public $name = 'Alunos';    

    public function beforeFilter(){
        parent::beforeFilter();
    }

    public function index() {}

    public function login(){
        debug($this->Auth->login());
        if ($this->request->is('post')) {
            if ($this->Auth->login()){
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash('Incorrect username and/or password.');
            }
        }
    }

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

    public function add() {
        debug($this->Auth->login());
            if($this->request->is('post')) { 
                if ($this->Aluno->save($this->request->data)) {
                    $this->Session->setFlash('Cadastrado.');
                }else {
                    $this->Session->setFlash('Falha no cadastro.');
                }
            }   
    }

    public function inicio() {
        debug($this->Auth->login());
    }

}

?>

我在 login.ctp 中的 debug($this->data) 结果:

array(
    'Aluno' => array(
        'password' => '*****',
        'username' => 'anyuser'
    )
)

我究竟做错了什么?

4

2 回答 2

0

将此代码添加到您的应用控制器:

function beforeFilter() {

    $this->Auth->userModel = 'Aluno'; <-- Should be singular. My mistake
    parent::beforeFilter();

} 

Cake2 的更新

// Place in beforeFilter() of AppController.php

$this->Auth->authenticate = array(
    'Form' => array(
        'userModel' => 'Aluno'
    )
);

您的问题是因为您没有告诉 cake 用户表使用什么。这就是为什么您的第一个实例有效,而第二个无效的原因。

于 2012-09-26T17:45:49.747 回答
0

改变这个:

echo $this->Form->create('Aluno', array('action' => 'login'));

至:

echo $this->Form->create('Alunos', array('url' => 'alunos/login'));

调用 Alunos Controller 的 login() 方法。

于 2017-10-27T11:15:20.453 回答