2013-12-09 31 views
0

我想爲我的應用程序添加一些基本身份驗證。所以我主要關注食譜教程,但我想用我的電子郵件而不是用戶名登錄。所以我寫了下面幾行:CakePHP無法通過電子郵件登錄

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; 
} 

我之前創建一個用戶,但我不能登錄。我究竟做錯了什麼?我越來越瘋狂......

+0

不解決問題。 – stevecross

+0

是否有錯誤或其他事情發生? – Dave

+0

僅限我在登錄方法中定義的Flash消息。 – stevecross

回答

2

按照CakePHP book數組應該是這樣的:

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

該死的

相反的'authenticate' => ...,我寫了'authentication' => ...

相關問題