【问题标题】:How to retrieve the Zend_Config used by Zend_Application?如何检索 Zend_Application 使用的 Zend_Config?
【发布时间】:2009-10-18 14:48:30
【问题描述】:
我正在迁移我的应用程序以使用 Zend_Application 进行引导。我曾经将 Zend_Config 作为我的 $registry 的键。
现在我不知道如何以这种方式使用它了(无需重新加载配置文件)。
我知道可以使用 $application->getOptions(),但这给了我一个数组,我必须在代码中的任何地方更改 $config 就像一个对象 ($config->services->storage) 到数组-like ($config['services']['storage'])。
【问题讨论】:
标签:
zend-framework
configuration
【解决方案1】:
Zend_Registry::set('config', new Zend_Config($this->getOptions()));
就是你所需要的。
【解决方案2】:
Zend_Application 仅使用(并存储)config.ini 文件,正如在调用 'new Zend_Application(ENV, '.../app.ini');' 的第二个参数中以数组形式给出的那样。为了能够将其存储为对象,您必须重新解析它。
您可以在引导程序中添加一个函数来执行此操作,并将其存储到注册表中,
<?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
}