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

你离开我真会死。 提交于 2019-12-09 15:18:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12887873/how-to-retrieve-parameters-from-zend-application-ini-file-during-the-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!