我想知道是否可以在 cakephp 的子控制器中继承/覆盖构造函数。
在我的AppController.php
我有这样的:
public function __construct( $request = null, $response = null ) {
parent::__construct( $request, $response );
$this->email = new CakeEmail();
$this->_constants = array();
$this->_constants['some_var'] = $this->Model->find( 'list', array(
'fields' => array( 'Model.name', 'Model.id' )
) );
}
在我的子控制器SomeController.php中,它继承了父构造函数
public function __construct( $request = null, $response = null ) {
parent::__construct( $request, $response );
}
当我尝试访问$this->email和$this->_constants['some_var']时,它们都为空。但是,只要我将代码直接放入 SomeController.php 而不是继承,它就起作用了。
我做错了什么还是蛋糕根本无法接受?我也对函数beforeFilter()进行了同样的尝试,同样的事情发生了。但是每个控制器都有自己的beforeFilter()是有道理的。