2012-07-24 78 views
0

在我的CakePHP應用程序中,當我使用另一個控制器的類時,出現Session讀取函數錯誤。CakePHP會話組件錯誤

example.com/app/getList作品以及
example.com/seconds/parseList作品以及
example.com/first/index給出了這樣的錯誤:

Fatal error: Call to a member function read() on a non-object in 
/example.com/app/Controller/AppController.php on line 468 

468線是這樣的:$list=$this->Session->read('myList');

我怎樣才能解決這個問題?

class FirstController extends AppController { 
    public function index() { 
     $Seconds = new SecondsController(); 
     $Seconds->parseList(); 
    } 
} 

class SecondsController extends AppController { 
    public $helpers = array('Html', 'Session'); 

    public function parseList() { 
     $output = $this->getList(); 
     return output; 
    } 
} 

class AppController extends Controller { 
    public $uses = array('App', 'Mydata'); 
    var $components = array('Session'); 
    public $helpers = array('Session','Html','Cache'); 

    public function getList() { 
     $list=$this->Session->read('myList'); 
     return $list; 
    } 
} 

編輯:我應該提到,example.com/app/getList運作良好。當我獨立運行此操作時,它不會給出會話錯誤。

+0

被AUTO_SESSION設置爲/app/config/core.php假? – 2012-07-24 12:46:38

回答

4

你必須在手動創建控制器的一個實例調用constructClasses()方法:

$Seconds = new SecondsController(); 
$Seconds->constructClasses(); 
+0

我的不好。我錯過了構造類。這對我有很大的幫助。非常感謝你。 – trante 2012-07-24 14:25:13

+0

當我們從Helper等其他地方調用控制器時會導致此錯誤。謝謝@dhofstet – 2016-09-14 07:50:34

0

你把var $components = array('Session');放在你的AppController中了嗎?

+0

是的,我已經做到了。 – trante 2012-07-24 13:23:42