Está en la página 1de 2

Zend Framework Module Config The Easy Way -, Personal ...

Zend Framework Module Config The Easy Way

sábado 12 de diciembre de 2009

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


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.

-PK- says:
miércoles 30 de diciembre de 2009
Hi Leonard, I want to thank you for this little tute. As you I looked around several times for a good solution to solve the module config problem. As you did I
read the blog posts by Jeroen and Matthijs and I liked them quite much, especially the one from Matthijs. Now I can get advantage also from your solution, it
faces the problem in a new and different way, probably even in a more natural one. Thank you again.
Chris says:
miércoles 24 de febrero de 2010
Thanks for sharing this! I like this solution, but I get an error when using your example code: Zend_Application_Bootstrap_Exception: Resource matching
"settings" not found in /path/to/library/Zend/Application/Bootstrap/BootstrapAbstract.php If I comment out $this-bootstrap('settings');, I can see the resource
"settings" in the array, but can't seem to access it. I created a clean project using zf tool (1.10.1), enabled modules in application.ini, but got the same error.
Any ideas? Cheers!

1 de 2 13/01/2011 12:26
Zend Framework Module Config The Easy Way -, Personal ...

Any ideas? Cheers!

Chris says:
miércoles 24 de febrero de 2010
Scrap my last comment regarding the error @_@ I had added [production] etc. to my module.ini which caused the error. This works well! I'm playing with
multiple layouts now... back to the playground...
sreknord says:
miércoles 24 de febrero de 2010
@chris, I did not implement ini sections but if you want you can. Just add the sections like production, development etc and change the code in the Module
Bootstrap class to use the given environment by adding it as the second parameter to Zend_Config_Ini.
don says:
lunes 1 de marzo de 2010
Thanks for this nice tut. plz explain how to integrate doctrine orm to moduleconfig!!
david says:
sábado 3 de abril de 2010
Hi there, I'm using a zend modular system but i don't know how to use Zend_Translate with it. I think your solution could be right for that so can you make an
example for using it ? Best regards
Witty Guy says:
domingo 18 de abril de 2010
Slick! Works like a champ! (on ZF v1.103) Nice solution, Leonard. Thanks!
yanick says:
viernes 14 de mayo de 2010
Hi Leonard, how would you "extend" an option from the application config? For inxtance, let's say your application.ini has a line like
resources.view.helperPath.ZendX_JQuery_View_Helper = 'ZendX/JQuery/View/Helper/' how would you add a new view helper path from your module
configuration file? I tried several ways to "merge" the application configurations with the "active module" configuration, but with no success; I never can
seem to find all the loaded resources...
Aurimas says:
lunes 31 de mayo de 2010
Okay tell me how can I achieve 'admin' module to use one layout resource settings and the 'frontend' another. If I try to put these settings in module.ini - one
setting overrides another and in admin module I get frontend layout :/ What I am missing here? I know i could use a plugin for this but its not what i want
when im thinking about module configs ... thoughts?
sreknord says:
miércoles 2 de junio de 2010
@yanick You can extend application options by using the getApplication method and then set the application options. for example: $options = $this-
getApplication()-getOptions(); $options['sreknord'] = ''; $this-getApplication()-setOptions($options);
sreknord says:
miércoles 2 de junio de 2010
@aurimas I know that a lot of people are thinking 'active' module stuff. There are quite a bit of post out there about that subject. This is not a post about that.
Here you can use settings that are in the scope of the module and not the application. Modules (and their settings) are all loaded at application Bootstrap.
Latishia says:
martes 13 de julio de 2010
I seem to be having the same issue as Chris had above, however I can't figure out how to fix it. I unzipped the files into the modules directory without editing
anything and all I get are errors. I have also tried commenting out settings, adding/removing sections in the ini file and nothing happens. I have just started
experimenting with the zend framework, been at it for about a week. I'm sure there is something simple I missing.
Latishia says:
martes 13 de julio de 2010
Nevermind, seems I had parts of the moduleconfig bootstrap added to another modules bootstrap. Seems I always find the answers once I give up and ask for
sims says:
jueves 19 de agosto de 2010
I was going to do it this way, but then you did it for me... :) I also like to dump a subset of settings into the registry. Then it's very quick to access those
settings. Cheers!

2 de 2 13/01/2011 12:26