如何在会话期间从Zend application.ini文件检索参数? [英] How to retrieve parameters from Zend application.ini file during the session?

查看:176
本文介绍了如何在会话期间从Zend application.ini文件检索参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

application.ini 是ZF中的配置文件。我有一些更多的设置,而不仅仅是那些ZF手册写的默认值。但是如何从我的动作控制器中检索那些参数,例如?

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?

Bootstrap_Abstract 类有 getOptions()方法返回一个读取 application.ini 文件的简单php数组:

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

it oop-style:

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

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

ZF有 Zend_Config_Ini 它读取 .ini '并完全返回ArrayObject。但我想避免在之后再次使用 Zend_Config_Ini 读取 application.ini Zend_Application 已经读过它。但 Bootstrap Zend_Application 类不会从 application.ini中提供一些自动创建ArrayObject

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.

第二,我可以在哪里存储这个$ config然后?在 Zend_Registry

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

但它似乎有点低效:我有一个副本在 Bootstrap 中以通常的PHP数组形式的 application.ini c> application.ini ,但是以 Zend_Registry 中的ArrayObject的形式。并且必须做两个步骤从我的配置获取一个参数。我如何更有效地解决这个问题?

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?

推荐答案

使用 Zend_Controller_Front ,您可以从<$ c $使用以下代码从应用程序的任何位置(控制器,插件,模型等)中创建应用程序c:application.ini :

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');

application.ini 这样的选项:

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

使用上述 $ config 变量的值:

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

希望有帮助。

这篇关于如何在会话期间从Zend application.ini文件检索参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆