How to Access Custom Config Settings Within Bootstrap - Zend Framework Application
Ok - so you have a Zend Framework ini file that contains the line:
mycustom.setting ="bar"
…and you want to be able to get the value of mycustom.setting from within an _init*() method of your Zend Framework MVC Application’s Bootstrap file.
Using getOptions() Method:
//With Zend 1.8.4 (as are all these examples)
//within bootstrap::_initX() method {
$options = $this->getOptions();
$mycustom_setting = $options['mycustom']['setting'];
Note The Case!
Note the first key of the ini setting is folded down to be lower case in the array returned by $this->getOptions()
If you have the following line in your config/application.ini file :
MyCustom.SetTing ="bar"
this, unhealthy, line of code:
print_r($this->getOptions());
will print out as:
...other values...
[mycustom] => Array
(
[SetTing] => bar
)
...other values including, possibly, db credentials...
Note Security!
DO NOT PRINT OUT THE OPTIONS ON A LIVE SITE!!
It would also print out the the db credentials if they are also set in the ini config!
Getting Cusom Ini Settings Using Bootstrap::getOption($key) method
Instead of using Bootstrap::getOptions(), try getting an option via the Bootstrap::getOption($key) method.
You access it like this:
;app ini file my.ini.setting.key = "value" --- //in bootstrap class i've not tested it though $myvalues_arr = getOption($key='my'); $myvalue = $myvalues_arr['ini']['setting']['key'];
Accessing the ini settings from a controller
For this setting:
mycustom.setting ="bar"
Try this in a controller class :
//within controller
$options = $this->getInvokeArg('bootstrap')->getOptions();
$mycustom_setting = $options['mycustom']['setting'];
echo($mycustom_setting);
prints:
bar
OK happy now?
Resources / Further Reading
See Zend Application API Docs - outlining the methods of the classes involved
See Zend Application API Docs With Tables Converted to a More Readable Format
Nabble question: “How to get Config options?“
December 22nd, 2009 at 12:18 pm
THX for that!