con*_*t01 6 authentication cakephp-2.0
在我看来,我有:
<?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(); ?>
Run Code Online (Sandbox Code Playgroud)
在我的AppController中:
public $components = array(
'Session',
'Auth'
);
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
Run Code Online (Sandbox Code Playgroud)
在我的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'));
}
}
}
Run Code Online (Sandbox Code Playgroud)
但登录不起作用,我错过了什么?
谢谢.
pen*_*egg 14
我相信问题是:
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
Run Code Online (Sandbox Code Playgroud)
这就是CakePHP 1.3中指定自定义登录字段的方式.CakePHP 2.0要求您在中指定这些字段public $components = array(...);.该1.3 API显示验证有$字段属性,但2.0 API显示,不再是$ Fields属性.所以你必须:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers
请告诉我它是如何工作的!
| 归档时间: |
|
| 查看次数: |
9243 次 |
| 最近记录: |