致命错误:在布尔值上调用成员函数user()

时间:2016-12-31 15:22:31

标签: cakephp fatal-error cakephp-3.3

我必须在视图中使用 loggedIn 用户数据,因此在AppController的{​​{1}}中创建变量 AUTH_USER 功能

beforeRender

每当出现错误时,不显示实际错误,而是显示没有样式格式的跟随错误

82     public function beforeRender(Event $event)
83     {
84          if ($this->Auth->user('id')) {
85          $this->set('AUTH_USER', $this->Auth->user());
86          }
87          ...
88          ...
89     }

删除行Fatal error: Uncaught Error: Call to a member function user() on boolean in /path_to_app/src/Controller/AppController.php:84 Stack trace: #0 /path_to_app/src/Controller/ErrorController.php(54): App\Controller\AppController->beforeRender(Object(Cake\Event\Event)) #1 /path_to_app/vendor/cakephp/cakephp/src/Event/EventManager.php(422): App\Controller\ErrorController->beforeRender(Object(Cake\Event\Event)) #2 /path_to_app/vendor/cakephp/cakephp/src/Event/EventManager.php(391): Cake\Event\EventManager->_callListener(Array, Object(Cake\Event\Event)) #3 /path_to_app/vendor/cakephp/cakephp/src/Event/EventDispatcherTrait.php(78): Cake\Event\EventManager->dispatch(Object(Cake\Event\Event)) #4 /path_to_app/vendor/cakephp/cakephp/src/Controller/ in /path_to_app/src/Controller/AppController.php on line 84 会显示实际错误

  

编辑2

AppController中的Auth组件加载代码initialize()

我编写了以下代码来加载84 - 86组件。

AUTH

2 个答案:

答案 0 :(得分:4)

我遇到了同样的错误,如果你将此行添加到beforeRender(),它应该有效:

$this->loadComponent('Auth');

可能是您未正确初始化initialize()函数中的组件。

答案 1 :(得分:0)

您没有为要检查的if语句提供正确的ID。您必须通过函数的参数发送登录用户的ID,然后在if语句中检查该ID。