Related download: Zend Framework Module Config
While I was looking for a way to setup configuration options form my modules I came across several ways to do so (like from Jeroen and Matthijs). There were some solutions I came up that also worked but those and the ones found online defeated the purpose of modularity. As I believe a module should just be dropped into any Zend Framework application without fuss or dependencies. Well, I have found out that it isn't that hard to do. The easy way is just to take these two simple steps:
1) Place a config.ini file in your module/configs directory and set the desired configuration options.
2) In your module bootstrap class create the following resource method.
protected function _initModuleConfig()
{
// load ini file
$iniOptions = new Zend_Config_Ini(dirname(__FILE__) . '/configs/module.ini');
// Set this bootstrap options
$this->setOptions($iniOptions->toArray());
}
That’s all you need to do; your configuration options are now available to your module.
Now, in you controller classes you can access your settings in the following way, just add the code below into your init method of the controller you wish to use the configuration options for (this is also shown in the related download). Don't forget to declare the protected $_bootstrap property though.
public function init()
{
// get app bootstrap
$front = Zend_Controller_Front::getInstance();
$bootstrap = $front->getParam('bootstrap');
// modules resource ArrayObject contains all bootstrap classes
// then get the bootstrap for this module (moduleconfig)
$this->_bootstrap = $bootstrap->getResource('modules')->offsetGet('moduleconfig');
}
And in your action methods you can then use:
// get all options from module bootstrap
$options = $this->_bootstrap->getOptions();
// get specific option
$option = $this->_bootstrap->getOption('user');
Unfortunately getting access to the module bootstrap class and it's options isn't easier, although I did see a function called getExecutedBootstraps() which works in the same way; it returns an ArrayObject with all bootstrap classes. So I ended up writing a utility class that makes above easier. An example method from this class could be something like the method below. In the related download you can see the full implementation of this class.
/**
* Get a bootstrap option
*
* @param string $option
* @return mixed
*/
public static function getOption($option)
{
$bootstrap = self::getBootstrap();
return $bootstrap->getOption($option);
}
To sum it up, setting up module configuration (through ini files) isn't that hard. All you do is load your file in your module bootstrap resource method and use the setOptions() method to set them. This way there are no dependecies, no (config)namespaces needed and no plugins or behavior that is required in your main application.
Sreknord.net is the personal web space of software engineer, Leonard Dronkers (NL). At Sreknord.net you will find interesting information about software engineering and web-development.
Software engineer, Leonard Dronkers is currently working as a web-developer at VNU Media (NL).
To get in touch with Leonard, please use the contact form.