在我看来,我有:
<?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中:
public $components = array(
'Session',
'Auth'
);
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
在我的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'));
}
}
}
但是登录不起作用,我错过了什么?
感谢。
答案 0 :(得分:14)
我认为问题是:
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
这就是CakePHP 1.3中指定自定义登录字段的方式。 CakePHP 2.0要求您在public $components = array(...);
中指定这些字段。 1.3 API显示Auth具有$ fields属性,但2.0 API表示不再有$ fields属性。所以你必须:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
可在以下网址找到更多信息:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers
请告诉我它是如何运作的!
答案 1 :(得分:0)
我的问题的最终解决方案。谢谢。
我遇到了userModel的问题,我写了这个:
'Auth' => array(
'userModel' => 'Member'
)
而不是:
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Member'
)
)
)