0

我无法在 CakePHP 2.1 中使用电子邮件登录,当我尝试使用用户名时,它工作得非常好。

下面是代码

查看:login.ctp

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

控制器:UsersController.php

<?php
    public function login(){
        if ($this->request->is('post')){
            if ($this->Auth->login()){
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid email and password'));
            }
        }
    }
?>      

型号:User.php

<?php
App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel {
    public function beforeSave(){
        if (isset($this->data[$this->alias]['password'])){
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }
}
?>  

数据库结构:用户表

id Auto_Increment
username
email
password
role

我不知道,为什么它不接受任何用于登录的电子邮件 ID,但对于用户名来说它工作得很好。

4

1 回答 1

3

阅读CakePHP 书的这一部分

这是该部分的示例,您需要配置表单身份验证以使用不同的字段作为用户名。

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);
于 2012-06-13T13:52:56.690 回答