2013-07-17 27 views
1

我是cakephp的新手,我試圖在代碼中使用$ 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'); 
     } 
    } 
} 

我能做些什麼來解決這個問題?

回答

3

檢查app/Controller/AppController.php中的'Session'元素在$components字段中。

var $components = array('Session'); 
2

首先,你必須在你的控制器代碼補充一點:

 var $components= array('Session'); 

它肯定會工作。