致命错误:在布尔值上调用成员函数user() [英] Fatal error: Call to a member function user() on boolean

查看:128
本文介绍了致命错误:在布尔值上调用成员函数user()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在视图中使用已登录用户数据,因此在 AppController AUTH_USER $ c>在 beforeRender 函数中。

I have to use loggedIn user data in view therefore creating a variable AUTH_USER in AppController in beforeRender function.

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     }

每当出现错误时,它不显示实际错误,而是显示以下错误,而没有样式格式

Whenever there is an error, instead of displaying actual error it shows following error without style formatting

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

删除行 84-86 显示实际错误


编辑2

Edit 2

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

我编写了以下代码来加载 AUTH 组件。

I have written the following code to load the AUTH component.

$this->loadComponent('Auth', [
   'authenticate' => [
      'Form' => [
         'userModel' => 'Admins',
            'fields' => [
               'username' => 'email', 
               'password' => 'password'
             ]
         ]
      ],
      'loginAction' => [
         'controller' => 'Admins',
         'action' => 'index'
      ],
      'loginRedirect' => [
         'controller' => 'Admins',
         'action' => 'contentList'
       ],
      'authError' => 'Did you really think you are allowed to see that?',
      'logoutAction' => [
         'controller' => 'Pages',
         'action' => 'home'
       ]
]);


推荐答案

我有相同的错误,如果添加此行到 beforeRender()应该起作用:

I had the same error and if you add this line to beforeRender() it should work:

$this->loadComponent('Auth');

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

这篇关于致命错误:在布尔值上调用成员函数user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆