【发布时间】:2010-03-29 14:14:49
【问题描述】:
我正在尝试将我的 Google Maps API 密钥存储在我的 application.ini 文件中,并且我希望能够在需要时从我的控制器中读取它。如何从我的控制器中读取 application.ini 中的值?
谢谢!
【问题讨论】:
标签: zend-framework web-applications configuration
我正在尝试将我的 Google Maps API 密钥存储在我的 application.ini 文件中,并且我希望能够在需要时从我的控制器中读取它。如何从我的控制器中读取 application.ini 中的值?
谢谢!
【问题讨论】:
标签: zend-framework web-applications configuration
This 文章可能会对您有所帮助。作者解释了如何从 application.ini 文件中检索 gmail 连接参数。
你可能想试试:
$bootstrap = $this->getInvokeArg('bootstrap');
$aConfig = $bootstrap->getOptions();
googleMapsKey = $aConfig['your_resource']['your_key'];
【讨论】:
我倾向于在引导程序中执行此操作,然后正如 Gordon 提到的那样将其扔到注册表中。然后,您可以从注册表中的任何位置获取它。
$this->config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
Zend_Registry::set('config', $this->config);
另外,就他的观点而言,控制器往往是错误的地方,除非你用多个键访问 API,我猜,但即使那样你也应该能够将逻辑放在模型中以选择注册表中的一个键。
【讨论】:
通过前端控制器
// bootstrap
$front->setParam('GMapsApiKey', 123456);
// controller
$this->getFrontController()->getParam('GMapsApiKey');
或注册表:
// bootstrap
Zend_Registry::set('GMapsApiKey', '123456');
// controller
Zend_Registry::get('GMapsApiKey');
但是,由于对 Google Maps API 的访问是在模型中进行的,因此您的控制器不需要知道 API 密钥。
【讨论】: