A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Zend Framework: Accessing configuration data in application.ini

Lately, I haven’t been doing any Zend Framework related things. Although currently tied up with other stuff, I check the community from time to time. Today, I found this quick and useful tip from Akrabat.

Akra talks about how you can access your configuration that you have set in your application.ini file – your configuration file.

Since it is a short post, I will copy-paste everything here.

Zend_Application will read the data in your application.ini and make it available from your bootstrap’s getOptions() method. It then sets the bootstrap as a parameter in the front controller. Note that the top level keys are all normalised to lowercase too.

You can then retrieve the options in a number of ways.

In the controller you can do this:

    public function someAction()
    {
        $bootstrap = $this->getInvokeArg('bootstrap'); 
        $options = $bootstrap->getOptions();
    }

Outside of the controller you can do this:

    $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    $options = $bootstrap->getOptions();

One downside is that $options is a nested array, and not a Zend_Config object. If you prefer to work with a Zend_Config object you need to create it yourself as Zend_Application discards the one it creates. The easiest way to do this is to create a new method in your bootstrap to create the Zend_Config object and store to the registry.

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

You can then get at your config data wherever you need it. Try not to depend too much on Zend_Registry keys though, as it can make testing harder.

Of course, the source is found here with all the comments from Akrabat’s readers.

This entry was posted in General. Bookmark the permalink.

3 Responses to Zend Framework: Accessing configuration data in application.ini

  1. e.s.t says:

    Helpful! Thanks.

  2. If I remember correctly, you can retrieve configuration.ini (When you are using (zend_application)) application resources you can get with it

    $this->bootstrap = $this->getInvokeArg('bootstrap');
    $this->bootstrap->getResource("keyName");

    but other I don’t remember other configuration samples… If I see again I will be post the code in here

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>