Dmitry Dmitry - 27 days ago 24
PHP Question

Access to module config in Zend Framework 2

How I can get access to my module config from the controller?

Answer

I am really surprised at how obscure this is, because I had exactly the same problem and could not find a definitive answer. One would think the ZF2 documentation would say something about this. Anyhow, using trial and error, I came across this extremely simple answer:

Inside controller functions:

$config = $this->getServiceLocator()->get('Config');

Inside Module class functions (the Module.php file):

$config = $e->getApplication()->getServiceManager()->get('Config');

whereas $e is an instance of Zend\Mvc\MvcEvent


In general, the config is accessible from anywhere you have access to the global service manager since the config array is registered as a service named Config. (Note the uppercase C.)

This returns an array of the union of application.config.php (global and local) and your module.config.php. You can then access the array elements as you need to.

Even though the OP is quite old now, I hope this saves someone the hour or more it took me to get to this answer.

Comments