Authentication AuthComponent不返回用户id
我已经做了很多次了,但这个问题从未发生过Authentication AuthComponent不返回用户id,authentication,cakephp-2.4,Authentication,Cakephp 2.4,我已经做了很多次了,但这个问题从未发生过 $this->Auth->user() $this->Auth->user('id')返回空值 有人吗 编辑 AppController中的AuthComponent定义: public $components = array( 'Session', 'Cookie', 'Auth' => array( 'authorize' => 'Controller', 'loginError'
$this->Auth->user()
$this->Auth->user('id')
返回空值
有人吗
编辑
AppController中的AuthComponent定义:
public $components = array(
'Session',
'Cookie',
'Auth' => array(
'authorize' => 'Controller',
'loginError' => 'Invalid account specified',
'authError' => 'No Permission',
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
),
);
编辑2
用户模型:
<?php
class User extends AppModel {
public $validate = array(
'_password' => array(
'equaltofield' => array(
'rule' => array('equaltofield', 'password'),
'message' => 'Require the same value to password.',
)
)
);
public function beforeValidate ($options = array()) {
$this->data[$this->alias]['password'] = AuthComponent :: password($this->data[$this->alias]['password']);
$this->data[$this->alias]['_password'] = AuthComponent :: password($this->data[$this->alias]['_password']);
}
function equaltofield($check,$otherfield) {
$fname = '';
foreach ($check as $key => $value){
$fname = $key;
break;
}
return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
}
}
?>
问题在于登录操作-
从$this->Auth->login()中删除$this->request->data
代码>
一定是-
public function login () {
if($this->request->is('post')) {
if($this->Auth->login()) {
if($this->Auth->user('role' == 'administrator')) {
return $this->redirect(array('controller' => 'brands', 'action' => 'index'));
}
else {
return $this->redirect(array('controller' => 'visits', 'action' => 'index'));
}
}
else {
$this->Session->setFlash('Login incorrect');
}
}
}
您是如何定义身份验证组件设置的?我用身份验证组件设置更新了我的问题。身份验证设置很好。。。这可能是缓存问题。。。。清空缓存文件夹…如果不起作用,请使用大量信息更新您的问题,如Users.php、userscocontroller.php…我清空了缓存,没有任何更改。。。用户已登录,授权有效,但除了用户名/密码之外,我无法检索当前用户的任何数据。我不知道用户控制器和模型代码有什么好处,但在那里,我也更新了我的问题。
public function login () {
if($this->request->is('post')) {
if($this->Auth->login()) {
if($this->Auth->user('role' == 'administrator')) {
return $this->redirect(array('controller' => 'brands', 'action' => 'index'));
}
else {
return $this->redirect(array('controller' => 'visits', 'action' => 'index'));
}
}
else {
$this->Session->setFlash('Login incorrect');
}
}
}