工大学子

zendframwork init() 继承

ZF中不可以使用__construct()构造方法,类的初始化操作需要用init()方法完成,

如果在每个controller的init()中,写一些重复的代码,当controller比较多时,一来比较麻烦,二来不方便后期维护。
怎样解决这个问题呢?可以把这些代码写到一个文件中,自定义一个父控制器。如:建立Global.php在其中写init(),

1 class HomeController extends GlobalController
2 {
3         public function indexAction()
4     {
5         $this->view->test= $this->test;
6     }  
7 }

但是,在自己有独立的init()内容的时候,父类的就不能用了,可以用parent::init();继承

public function init()
{ 
     Zend_Layout::startMvc(array(
     'layout' => 'layouts'
     ));
    parent::init();
}

 

posted @ 2013-05-27 17:03  飞天小莫  阅读(169)  评论(0编辑  收藏  举报
飞天小莫