Zend Framework: Accessing configuration data in application.ini

Posted on: Dec 29, 2009 by wenbert

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.


Subscribe to comments Comment | Trackback |
Post Tags:

Browse Timeline


Comments ( 3 )

Helpful! Thanks.

e.s.t added these pithy words on Dec 29 09 at 2:58 PM

Thanks to Akrabat :P

Wenbert added these pithy words on Dec 29 09 at 7:13 PM

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

Murat BEŞER added these pithy words on Dec 30 09 at 12:52 AM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!