Accessing your configuration data that's stored in application.ini

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.

14 Responses to “Accessing your configuration data that's stored in application.ini”

  1. 1 Tobi

    I used the same way to make my config available. I also set the db adapter in Zend_Registry.

    Regards
    Tobi

  2. 2 Anton Shevchuk

    Thanks for this is solution

  3. 3 Jon Whitcraft

    Rob,

    Thanks I was just looking for how to get access to the options in a view helper with out using Zend_Registry.

    Thanks!

  4. 4 Jon Whitcraft

    You have one two many ')' on your second example.

  5. 5 Rob...

    Thanks Jon,

    Fixed :)

    Rob...

  6. 6 bin

    Hello
    Thanks for your post.
    Help me. How to access database ressources in application.ini and write to Zend_Registry ?

    Thanks a lot

  7. 7 Rob...

    bin,


    protected function _initDatabaseRegistry()
    {
    $this->bootstrap("db");
    $db = $this->getResource("db");
    Zend_Registry::set('db', $db);
    return $db;
    }

    Regards,

    Rob...

  8. 8 Neck

    Note that you can actually combine bootstrap's registry with zend registry (why having duplicates ?)

    protected function _initRegistry() { $this->setContainer(Zend_Registry::getInstance());
    }

  9. 9 Steve P. Sharpe

    Hi Rob,

    Thanks for this, it really helped!

    Bookmarked...

  10. 10 takehsin

    And what about module configuration? Can we have /mymodule/configs/config.ini in which we can put the same options as in application.ini? e.g. resources.session., resources.mail. etc.

  11. 11 picha

    Why do you return stuff from _initXXX() methods?
    I've seen it done in Zend's own documentation but haven't seen anything actually use those return values. Does that come into play in unit testing, maybe?

  12. 12 Rob...

    Picha,

    when you return something from an _initXX(), it becomes available in the bootstrap for use later.

    Regards,

    Rob...

  13. 13 Jeremy Glover

    Thanks for this Rob. I tried the bootstrap approach and it worked like a charm.

  14. 14 Markus

    Thanks for that nice bootstrap snippet!

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.