2012-07-24 41 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