29

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());
}
Cameron
  • 27,963
  • 100
  • 281
  • 483

1 Answers1

66

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.

Mark Northrop
  • 2,613
  • 1
  • 20
  • 21
  • 2
    Thanks :) I thought Session was a helper and included as that instead of a component! – Cameron Feb 02 '11 at 16:17
  • CakePHP provides both: SessionComponent for using the session in the controller, and SessionHelper for using it in the views. – dhofstet Feb 02 '11 at 16:28
  • 4
    Actually, there is also a Session helper. You need to use the Session component when you want to do things with the session in the controller and you use the Session helper to do things with your session in the view. If you're setting a flash message with the Session component in your controller, you're going to have to use the Session helper to display it in your view. – Mark Northrop Feb 02 '11 at 16:30
  • I ran into this problem too when using the cakephp blog example. I'm not sure if enabling the DebugKit toolbar removed the session or not. With the help of this answer, I appended Session to the array already there: `class AppController extends Controller { public $components = array('DebugKit.Toolbar','Session'); }` – Carlos Apr 25 '14 at 12:41
  • Can we prevent the any property from overriding? like we can do something like parent::anyMethod(); – Sagar Guhe Oct 13 '14 at 08:47