开发者

Zend Framework - Reading Application.ini values from a Controller

开发者 https://www.devze.com 2022-12-24 19:47 出处:网络
I am trying to store my Google Maps API Key in my application.ini file, and I would like to be able to read it from my controller whenever its needed.How can I go about reading value开发者_StackOverfl

I am trying to store my Google Maps API Key in my application.ini file, and I would like to be able to read it from my controller whenever its needed. How can I go about reading value开发者_StackOverflow社区s from the application.ini from my controller?

Thanks!


This article might help you out. The author explains how to retrieve gmail connection params from the application.ini file.

You might want to try:

$bootstrap = $this->getInvokeArg('bootstrap');
$aConfig = $bootstrap->getOptions();
googleMapsKey = $aConfig['your_resource']['your_key'];


I tend to do this in the bootstrap, and then as Gordon mentions toss it in the registry. You can then get it anywhere out of the registry.

$this->config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
Zend_Registry::set('config', $this->config);

Also, to his point, the controller would tend to be the wrong place for it, unless you are accessing the API with multiple keys, I guess, but even then you should be able to place the logic in the model to select a key from the registry.


Either through the FrontController

// bootstrap
$front->setParam('GMapsApiKey', 123456);

// controller
$this->getFrontController()->getParam('GMapsApiKey');

or Registry:

// bootstrap
Zend_Registry::set('GMapsApiKey', '123456');

// controller
Zend_Registry::get('GMapsApiKey');

However, since access to the Google Maps API is something that happens in the model, your controller should not need to know the API key.

0

精彩评论

暂无评论...
验证码 换一张
取 消