zend-frameworkconfigurationzend-configzend-app-bootstrap

How to retrieve parameters from Zend application.ini file during the session?


Possible Duplicate:
How could read application.ini on controller using zend framework

application.ini is a configuration file in ZF. I have there some more settings than just those defaults that ZF manual writes about. But how may I retrieve those parameters from it from my action controllers for example? And where is it better to store this config during the session?

The Bootstrap_Abstract class has getOptions() method which returns a simple php array of read application.ini file:

$app = new Zend_Application(APPLICATION_ENV, '/application.ini');
$config = $app->bootstrap()->getOptions();  // $config is a php array of application.ini

And I'd like to get element form it oop-style:

$param = $config[one][two];  // vs.
$param = $config->one->two;  // like this

ZF has Zend_Config_Ini class which reads .ini's and returns exactly the ArrayObject. But I'd like to avoid reading application.ini with Zend_Config_Ini one more time after Zend_Application has anyway already read it. But Bootstrap or Zend_Application classes don't provide some automatic creation of ArrayObject from application.ini.

And the second, where may I store this $config then? In Zend_Registry?

Zend_Registry::set('config', $config);

And then in my some action controller I retrieve it:

$config = Zend_Registry::get('config');  // I retrieve config
$param = $config->one->two;              // I retrieve parameter from it and use it

But it seems a little inefficient: I have one copy of application.ini in the Bootstrap in the form of usual php array and one copy of the same application.ini but in the form of ArrayObject in Zend_Registry too. And have to make two steps to get a parameter from my config. How can I solve this problem more efficiently?


Solution

  • Using Zend_Controller_Front, you can retrieve information from application.ini from anywhere in your application (controller, plugin, models etc) using code like this:

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

    Say in application.ini you had some options like this:

    apikeys.google.maps.id  = "abc"
    apikeys.google.maps.key = "123456789"
    apikeys.twitter.oauth   = "20jg9032jg9320gj30"
    

    You can then access those values using the above $config variable:

    $apikeys = $config->getOption('apikeys');
    $mapsId  = $apikeys['google']['maps']['id'];  // abc
    $maksKey = $apikeys['google']['maps']['key']; // 123456789
    $twitKey = $apikeys['twitter']['oauth'];      // 20jg9032jg9320gj30
    

    Hope that helps.