views:

26

answers:

2

Maybe the question is not self-explanatory, so I will explain it through.

The deal is: I got the variable $conn in the bootstrap class file. I'd like to make it global for every controller so that I just have to call $this->conn in the controller action scope in order to access the data inside. How would I do it?

Thx

A: 

One fairly straightforward way is to create your own base class form which your controller's inherit:

<?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!
}
timdev
A: 

Matthew Weier O'Phinney posted a blog entry recently with some examples of how to use action helpers to do this, see:

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

this will achieve the same thing without having to use a base controller class.

Tim Fountain

related questions