This is a very old post but since no answer has been accepted and I recently needed to achieve the same thing, I thought I'd share my solution(s).
I discovered two possible ways of achieving what you want, theThe reason I needed to access the ServiceManager before the Bootstrap event is triggered, was so I could manipulate the merged configuration with values retrieved from the database.
Solution 1:
public function onLoadModulesPost(ModuleEvent $event) { /* @var $serviceManager \Zend\ServiceManager\ServiceManager */ $serviceManager = $event->getParam('ServiceManager'); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceManager->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); $configListener->setMergedConfig($configuration); $event->setConfigListener($configListener); $serviceManager->setAllowOverride(true); $serviceManager->setService('Config', $configuration); $serviceManager->setAllowOverride(false); } Solution 2:
If manipulating the merged configuration is your end goal (while having access to the ServiceManager) then there might be a simpler way.
In your module configuration file, override the Config factory definition.
return [ ... 'service_manager' => [ 'factories' => [ ... 'Config' => 'Your\Mvc\Service\ConfigFactory', ], ], ]; By doing so, every time the configuration is retrieved from the ServiceManager your instance will be instantiated and the cached value will be returned.
Your factory class may look something like:
<?php namespace Your\Mvc\Service; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; class ConfigFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { /* @var $moduleManager \Zend\ModuleManager\ModuleManager */ $moduleManager = $serviceLocator->get('ModuleManager'); $moduleManager->loadModules(); $event = $moduleManager->getEvent(); $configListener = $event->getConfigListener(); $configuration = $configListener->getMergedConfig(false); $someService = $serviceLocator->get('Your/Custom/Service'); $information = $someService->fetchSomeInformation(); $configuration = array_merge($configuration, $information); return $configuration; } }