CakePHP无法使用电子邮件登录

时间:2013-12-09 21:01:50

标签: php cakephp cakephp-2.4

我想为我的应用添加一些基本身份验证。所以我大多数都遵循了食谱教程,但我想用我的电子邮件而不是用户名登录。所以我写了以下几行:

AppController.php

public $components = array(
  'Auth' => array(
    'authentication' => array(
      AuthComponent::ALL => array(
        'fields' => array('username' => 'email')
      )
    )
  )
);

UsersController.php

public function login() {
  if ($this->request->is('post')) {
    if ($this->Auth->login()) {
      $this->redirect($this->Auth->redirectUrl());
    }
    $this->Session->setFlash(__('Invalid email/password combination'));
  }
}

login.ctp

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

user.php的

public function beforeSave($options = array()) {
  $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
  return true;
}

之前我创建了一个用户,但我无法登录。我究竟做错了什么?我疯了......

2 个答案:

答案 0 :(得分:2)

根据CakePHP book,数组应如下所示:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

答案 1 :(得分:0)

DAMN IT

而不是'authenticate' => ...,我写了'authentication' => ...

相关问题