20111007

Zend Framework: How to access your configuration from the application.ini file

There are many ways of getting the config from our application.ini file. The usual way is to load it using Zend_Config in our Boostrap class. The other way is to load it from a parameter in the front controller (Zend_Application put it there).

Using Bootstrap
Add this to your Boostra.php file:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
//...
protected function _initConfig(){
$config_options = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config_options);
return $config_options;
}
}
view raw Bootstrap.php hosted with ❤ by GitHub


You can fetch the config wherever you want using: 
$config = Zend_Registry::get("config");
Loading from Front controller
From a View action:
<?php
class MyController extends Zend_Controller_Action {
//...
public function myAction(){
$bootstrap = $this->getInvokeArg('bootstrap');
$config_options = $bootstrap->getOptions();
}
}
view raw gistfile1.aw hosted with ❤ by GitHub

From outside the controller:
<?php
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$config_options = $bootstrap->getOptions();
view raw gistfile1.aw hosted with ❤ by GitHub

No hay comentarios.:

Publicar un comentario