使用电子邮件,而不是用户名CakePHP的2.0认证 [英] Cakephp 2.0 Authentication using email instead of username
问题描述
在我看来,我有:
<?php
echo $this->Form->create('User', array("controller" => "Users", "action" => "login", "method" => "post"));
echo $this->Form->input('User.email', array("label" => false));
echo $this->Form->input('User.password', array("label" => false, 'class' => 'password-input'));
echo $this->Form->end(); ?>
在我AppController的:
In my AppController:
public $components = array(
'Session',
'Auth'
);
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
在我的UsersController:
In my UsersController:
function beforeFilter(){
$this->Auth->allow('sign_up', 'login', 'logout', 'forgot_password');
return parent::beforeFilter();
}
public function login() {
if ($this->Auth->login()) {
$this->Session->setFlash(__('Successfully logged in'), 'default', array('class' => 'success'));
$this->redirect($this->Auth->redirect());
} else {
if (!empty($this->request->data)) {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array('class' => 'notice'));
}
}
}
但登录不工作,我错过什么?
But the login is not working, what am I missing?
感谢。
推荐答案
我相信问题是:
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
这是自定义登录领域是如何在CakePHP中规定的1.3。 CakePHP的2.0,而不是要求您在来指定这些领域的公共$组件=阵列(...);
。该 1.3 API 表明,验证有一个字段$财产,但的2.0 API 表明不再有一个$字段属性。所以,你必须:
That was how custom login fields were specified in CakePHP 1.3. CakePHP 2.0 instead requires you to specify these fields in the public $components = array(...);
. The 1.3 API shows that Auth has a $fields property, but the 2.0 API shows that there is no longer a $fields property. So you must:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
更多信息,请访问:<一个href=\"http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers\">http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers
请告诉我它是如何工作!
Please tell me how it works out!
这篇关于使用电子邮件,而不是用户名CakePHP的2.0认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!