PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Fatal error: Call to a member function user() on boolean

 January 23, 2022     cakephp, cakephp-3.3, fatal-error     No comments   

Issue

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

Removing line 84 - 86 displays actual error

Edit 2

code for Auth component load in AppController initialize()

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'
       ]
]);

Solution

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

$this->loadComponent('Auth');

It could be that you are not properly initializing the component in the initialize() function.



Answered By - nimrod
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing