Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the configuration of a Zend Framework application from a controller?

Tags:

I have a Zend Framework application based on the quick-start setup.

I've gotten the demos working and am now at the point of instantiating a new model class to do some real work. In my controller I want to pass a configuration parameter (specified in the application.ini) to my model constructor, something like this:

class My_UserController extends Zend_Controller_Action {     public function indexAction()     {         $options = $this->getFrontController()->getParam('bootstrap')->getApplication()->getOptions();         $manager = new My_Model_Manager($options['my']);         $this->view->items = $manager->getItems();     } } 

The example above does allow access to the options, but seems extremely round-about. Is there a better way to access the configuration?

like image 703
Adam Franco Avatar asked Nov 05 '09 14:11

Adam Franco


People also ask

How do I load a view in Zend Framework?

The render() function will render the given view script within the variable scope of the script is was called from. $this->partial('script. phtml', array('var1' => 'value 1', 'var2' => 'value 2'));

How do I know what version of Zend Framework I have?

The static method Zend\Version\Version::getLatest() provides the version number of the last stable release available for download on the site Zend Framework.


2 Answers

I always add the following init-method to my bootstrap to pass the configuration into the registry.

protected function _initConfig() {     $config = new Zend_Config($this->getOptions(), true);     Zend_Registry::set('config', $config);     return $config; } 

This will shorten your code a little bit:

class My_UserController extends Zend_Controller_Action {     public function indexAction()     {         $manager = new My_Model_Manager(Zend_Registry::get('config')->my);         $this->view->items = $manager->getItems();     } } 
like image 175
Stefan Gehrig Avatar answered Oct 01 '22 04:10

Stefan Gehrig


Since version 1.8 you can use the below code in your Controller:

$my = $this->getInvokeArg('bootstrap')->getOption('my'); 
like image 41
yarson Avatar answered Oct 01 '22 03:10

yarson