Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to module config in Zend Framework 2

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

like image 932
Dmitry Avatar asked Jan 21 '12 22:01

Dmitry


People also ask

How do I connect to a database in Zend Framework 2?

$adapter = $this->getServiceLocator()->get('adapter'); Create a sql statetment and put in variable $sql. Now do this: $statement = $adapter->createStatement($sql); $result = $statement->execute();

Is Zend framework in PHP?

Zend is an open source PHP framework. It is pure object-oriented and built around the MVC design pattern. Zend framework contains collection of PHP packages which can be used to develop web applications and services. Zend was started by Andi Gutmans and Zeev Suraski.


2 Answers

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.

like image 157
tomo Avatar answered Sep 18 '22 20:09

tomo


What exactly do you want to do in your controller with the module configuration? Is it something that can't be done by having the DI container inject a fully configured object into your controller instead?

For example, Rob Allen's Getting Started with Zend Framework 2 gives this example of injecting a configured Zend\Db\Table instance into a controller:

return array( 'di' => array(     'instance' => array(         'alias' => array(             'album' => 'Album\Controller\AlbumController',         ),         'Album\Controller\AlbumController' => array(             'parameters' => array(                 'albumTable' => 'Album\Model\AlbumTable',             ),         ),         'Album\Model\AlbumTable' => array(             'parameters' => array(                 'config' => 'Zend\Db\Adapter\Mysqli',         )),         'Zend\Db\Adapter\Mysqli' => array(             'parameters' => array(                 'config' => array(                     'host' => 'localhost',                     'username' => 'rob',                     'password' => '123456',                     'dbname' => 'zf2tutorial',                 ),             ),         ),         ... 

If you need to do additional initialization after the application has been fully bootstrapped, you could attach an init method to the bootstrap event, in your Module class. A blog post by Matthew Weier O'Phinney gives this example:

use Zend\EventManager\StaticEventManager, Zend\Module\Manager as ModuleManager  class Module {     public function init(ModuleManager $manager)     {         $events = StaticEventManager::getInstance();         $events->attach('bootstrap', 'bootstrap', array($this, 'doMoarInit'));     }      public function doMoarInit($e)     {         $application = $e->getParam('application');         $modules     = $e->getParam('modules');          $locator = $application->getLocator();         $router  = $application->getRouter();         $config  = $modules->getMergedConfig();          // do something with the above!     } } 

Would either of these approaches do the trick?

like image 25
Jason Grimes Avatar answered Sep 18 '22 20:09

Jason Grimes