Access to module config in Zend Framework 2

前端 未结 8 2115
清酒与你
清酒与你 2021-01-30 21:05

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

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 21:42

    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.

提交回复
热议问题