Oop 子控制器中的cakephp重写构造

Oop 子控制器中的cakephp重写构造,oop,class,cakephp,inheritance,constructor,Oop,Class,Cakephp,Inheritance,Constructor,我想知道是否有可能在cakephp中继承/重写子控制器中的构造函数 在我的AppController.php中 我是这样的: public function __construct( $request = null, $response = null ) { parent::__construct( $request, $response ); $this->email = new CakeEmail(); $this->_constants = arra

我想知道是否有可能在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->\u常量['some\u var']时,它们都是空的。但是当我将代码直接放入SomeController.php而不是继承时,它就工作了

是我做错了什么事,还是这根本不容易让人接受? 我还尝试了同样的函数beforeFilter(),同样的事情发生了。
但是每个控制器都有自己的beforeFilter()

是有道理的,我甚至不会尝试重写appController的
\u构造”函数。这就是
beforeFilter
beforeRender`方法的用途。看起来您只是试图从appController向每个控制器传递变量。你可以这样做

class AppController extends Controller {

   var $_constants = array();

   public function beforeFilter(){
      $this->_constants[] = array('this', 'that', 'the other');
   }

}
class UsersController extends AppController {

   public function add(){
      pr($this->_constants);
   }
}
在模型的控制器中,你可以像这样访问变量

class AppController extends Controller {

   var $_constants = array();

   public function beforeFilter(){
      $this->_constants[] = array('this', 'that', 'the other');
   }

}
class UsersController extends AppController {

   public function add(){
      pr($this->_constants);
   }
}
如果您试图将变量发送到视图(稍微),则情况就不同了。只需使用set方法

class AppController extends Controller {

   public function beforeFilter(){
      $this->set('_constants', array('this', 'that', 'the other'));
   }

}

在任何视图中,您都可以使用
pr($\u常量)调用
\u常量
变量。因为它位于appController中,所以它应该在每个视图上都可用

我甚至不会尝试重写appController的
\u构造”函数。这就是
beforeFilter
beforeRender`方法的用途。看起来您只是试图从appController向每个控制器传递变量。你可以这样做

class AppController extends Controller {

   var $_constants = array();

   public function beforeFilter(){
      $this->_constants[] = array('this', 'that', 'the other');
   }

}
class UsersController extends AppController {

   public function add(){
      pr($this->_constants);
   }
}
在模型的控制器中,你可以像这样访问变量

class AppController extends Controller {

   var $_constants = array();

   public function beforeFilter(){
      $this->_constants[] = array('this', 'that', 'the other');
   }

}
class UsersController extends AppController {

   public function add(){
      pr($this->_constants);
   }
}
如果您试图将变量发送到视图(稍微),则情况就不同了。只需使用set方法

class AppController extends Controller {

   public function beforeFilter(){
      $this->set('_constants', array('this', 'that', 'the other'));
   }

}

在任何视图中,您都可以使用
pr($\u常量)调用
\u常量
变量。因为它位于appController中,所以它应该在每个视图上都可用

如果您计划覆盖构造函数,您应该首先查看您正在覆盖的内容。以及确切的构造函数参数是什么样的。。。提示:字体提示应该得到尊重。你忘了设置它。。??尝试$this->set方法。这看起来像是一个可怕的错误混乱。该任务仍然有一个EmailComponent,在构造函数中进行查找是很尴尬的。这看起来是一个完全错误的方法,不管它被认为是什么。如果你计划覆盖构造函数,你应该首先看看你在那里覆盖了什么。以及确切的构造函数参数是什么样的。。。提示:字体提示应该得到尊重。你忘了设置它。。??尝试$this->set方法。这看起来像是一个可怕的错误混乱。该任务仍然有一个EmailComponent,在构造函数中进行查找是很尴尬的。这看起来像是一个完全错误的方法,不管它被认为是使用什么。