$this->;会话->;setFlash()在cakehp2.3.7中显示一个错误


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

我是cakepp的新手,我正试图在代码中使用$this->Session->setFlash(),它说有一个错误:在非对象上调用成员函数setFlash(!这是我的代码

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

我能做些什么来解决这个问题?

在app/Controller/AppController.php中检查$components字段中是否有"Session"元素。

  var $components =  array('Session');

首先,您必须将其添加到控制器代码中:

     var $components= array('Session');

它肯定会起作用。