我正在尝试实施Audit Trail插件 - https://github.com/robwilkerson/CakePHP-Audit-Log-Plugin
这一切都很好,但我无法通过按照说明获得用户身份验证,我收到以下错误 -
Fatal error: Call to undefined method CakeErrorController::currentUser()
我已按照说明添加
protected function currentUser() {
$user = $this->Auth->user();
return $user[$this->Auth->userModel]; # Return the complete user array
}
并添加
public function beforeFilter() {
...
if( !empty( $this->data ) && empty( $this->data[$this->Auth->userModel] ) ) {
$this->data[$this->Auth->userModel] = $this->currentUser();
}
}
到我的appController,有没有人以前实现过这个或识别错误?
答案 0 :(得分:1)
对于Cakephp 2.4,您必须进行一些更改才能使用Auth组件:
在AppModel中:
public function currentUser() {
$userId = AuthComponent::user('id');
//Get the information of the user
$currentUser = $this->importModel('User')->find('first', array(
'conditions'=>array('User.id'=>$userId),
));
//Return all the User
return $currentUser['User'];
}
现在在AppController中: 事实上,您不需要在控制器中执行任何其他操作,只是为了防止出现问题。所以,可选:
if( !empty( $this->request->data ) && empty( $this->request->data[$this->Auth->userModel] ) ) {
$user['User']['id'] = $this->Auth->user('id');
$this->request->data[$this->Auth->userModel] = $user;
}
它对我有用。
答案 1 :(得分:0)
不要将currentUser()
功能添加到AppController
,它必须位于AppModel
。这是我的currentUser()
函数使用CakePHP 2.3的样子:
public function currentUser() {
return array('id' => AuthComponent::user('id'));
}