Henrique Vicente
Henrique Vicente

Reputation: 225

How to retrieve the Zend_Config used by Zend_Application?

I'm migrating my app to use Zend_Application for bootstrap. I used to have the Zend_Config as a key for my $registry.

Now I don't know how to use it this way anymore (without having to reload the config file). I know it's possible to use $application->getOptions(), but that gives my an array and I would have to change everywhere in my code where $config is used like an object ($config->services->storage) to array-like ($config['services']['storage']).

Upvotes: 3

Views: 751

Answers (2)

Benjamin Cremer
Benjamin Cremer

Reputation: 4822

Zend_Registry::set('config', new Zend_Config($this->getOptions()));

is all you need.

Upvotes: 4

Alister Bulman
Alister Bulman

Reputation: 35139

Zend_Application only uses (and stores) the config.ini file, as given in the second parameter of the call to 'new Zend_Application(ENV, '.../app.ini');' as an array. To be able to store it as an object, you'll have to re-parse it.

You can add a function into the bootstrap to do so, and store it into the registry,

<?php
// in 'class Bootstrap extends Zend_Application_Bootstrap_Bootstrap'

protected function _initConfig()
{
    // config
    //#Zend_Registry::set('config', $this->getOptions());  // as an array

    $ZFOptions      = array(); // could be: 'allowModifications'=>true
    $section        = 'production';  // or from the environment
    $configFilename = APPLICATION_PATH .'/configs/application.ini';
    $config = new Zend_Config_Ini($configFilename, $section, $ZFOptions);
    Zend_Registry::set('config', $config);  // as an object
}

Upvotes: 2

Related Questions