Sopheakdey Moeun
Sopheakdey Moeun

Reputation: 121

$this->Session->setFlash() display an error in cakephp 2.3.7

I'm new to cakephp and i'm trying to use $this->Session->setFlash() in my code and it says that there is an error: Call to a member function setFlash() on a non-object! Here's my code

function add(){
    if(!empty($this->data)){
        if($this->Post->save($this->data)){               
            $this->Session->setFlash('The post was successfully added');
            $this->redirect(array('action'=>'index'));
        }else{
            $this->Session->setFlash('The post was not saved, please try again');
        }
    }
}

What can i do to solve this problem?

Upvotes: 1

Views: 5798

Answers (2)

shah
shah

Reputation: 143

At first you have to add this in your controller code:

     var $components= array('Session');

It will work definitely.

Upvotes: 2

Vadim
Vadim

Reputation: 642

Check in app/Controller/AppController.php you have 'Session' element in $components field.

  var $components =  array('Session');

Upvotes: 3

Related Questions