如何自动设置和属性到控制器

时间:2010-05-10 03:56:06

标签: zend-framework controller scope global

也许问题不是不言自明的,所以我将通过解释。

交易是:我在bootstrap类文件中得到了变量$ conn。我想让每个控制器全局化,以便我只需要在控制器操作范围内调用$ this-> conn以访问其中的数据。我该怎么做?

THX

2 个答案:

答案 0 :(得分:0)

一种相当直接的方法是创建自己的基类表单,控制器继承该表单:

<?PHP

class My_Controller_Action extends Zend_Controller_Action {
    public $conn;

    public function init(){
      //set $this->conn 
    }
}

class Some_Real_Controller extends My_Controller_Action {
    //$this->conn exists!
}

class Some_Other_Real_Controller extends My_Controller_Action {
    //$this->conn exists here too!
}

答案 1 :(得分:0)

Matthew Weier O'Phinney最近在博客文章中发布了一些如何使用动作助手来做这个的例子,请参阅:

http://weierophinney.net/matthew/archives/235-A-Simple-Resource-Injector-for-ZF-Action-Controllers.html

这将实现相同的功能,而无需使用基本控制器类。

相关问题