Reputation: 18109
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 values from the application.ini from my controller?
Thanks!
Upvotes: 5
Views: 10651
Reputation: 1385
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.
Upvotes: 5
Reputation: 5856
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'];
Upvotes: 5
Reputation: 316969
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.
Upvotes: 3