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.
The 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.
Problem:
The example found in the Zend documentation shows how to manipulate the merged configuration, but at that particular time the Service manager is empty, making it impossible to retrieve things like database adapters etc.
Solution:
In your module class, implement the interface InitProviderInterface and add the appropriate method.
public function init(ModuleManagerInterface $moduleManager) { $eventManager = $moduleManager->getEventManager(); $eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'onLoadModulesPost']); } The EVENT_LOAD_MODULES_POST event will get invoked after the EVENT_MERGE_CONFIG event but before the EVENT_BOOTSTRAP event is triggered. Also at this particular time the ServiceManager will contain all the factories, invokable classes you're wanting to access.
Your callback method may look something like.
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); }