Home » Php » php – CakePHP: Call to a member function setFlash() on a non-object

php – CakePHP: Call to a member function setFlash() on a non-object

Posted by: admin April 23, 2020 Leave a comment

Questions:

I get the following error when trying to logout of my CakePHP app:

Notice (8): Undefined property: UsersController::$Session [APP/controllers/users_controller.php, line 75]
Fatal error: Call to a member function setFlash() on a non-object in /Users/cameron/Sites/cakeapp/app/controllers/users_controller.php on line 75

This is the code for lines 74, 75 and 76:

function logout() {
    $this->Session->setFlash('Good-Bye');
    $this->redirect($this->Auth->logout());
}
How to&Answers:

It looks like you don’t have the Session component loaded in your Users controller.

The Session component should be loaded by default, but if you’ve set the components array in AppController this will overwrite the defaults.

This means that if you have

var $components = array();

in your AppController, make sure the Session component is included there:

var $components = array('Session');

Alternatively, you can load the Session component in your Users controller if you don’t want to use it app-wide.