title: Dynamically loading ZF2 Modules
body: <p>I have following application.config</p>
  
  <pre><code>return array(
  'modules' =&gt; array(
  
      'Application',
      'ErrorHandler'
  ),
  'module_listener_options' =&gt; array(
      'module_paths' =&gt; array(
          './module',
          './vendor'
      ),
      'config_glob_paths' =&gt; array(
          'config/autoload/{,*.}{global,local}.php'
      )
  )
  );
  </code></pre>
  
  <p>and in the Application/Module.php I have (few of the functions):</p>
  
  <pre><code>    public function onBootstrap(MvcEvent $e)
  {
      $eventManager        = $e-&gt;getApplication()-&gt;getEventManager();
      $moduleRouteListener = new ModuleRouteListener();
      $moduleRouteListener-&gt;attach($eventManager);
  
      $this-&gt;initModules($e);
  }
  
  public function getConfig()
  {
      return include __DIR__ . '/config/module.config.php';
  }
  
  public function getAutoloaderConfig()
  {
      return array(
          'Zend\Loader\StandardAutoloader' =&gt; array(
              'namespaces' =&gt; array(
                  __NAMESPACE__ =&gt; __DIR__ . '/src/' . __NAMESPACE__,
              ),
          ),
      );
  }
  
  private function getModules(MvcEvent $e) {
      $sm = $e-&gt;getApplication()-&gt;getServiceManager();
      $moduleTable = $sm-&gt;get('ModuleTable');
  
      $modules = array();
  
      foreach ($moduleTable-&gt;fetchAll() as $m) {
          $modules[] = $m;
      }
  
      return $modules;
  }
  
  private function initModules(MvcEvent $e) {
      $modules = $this-&gt;getModules($e);
  
      $serviceManager = $e-&gt;getApplication()-&gt;getServiceManager();
      $moduleManager = $serviceManager-&gt;get('ModuleManager');
  
      $loadedModules = $moduleManager-&gt;getLoadedModules();
  
      foreach ($loadedModules as $module) {
          $this-&gt;loadedModules[] = str_replace('\Module', '', get_class($module));
      }
  
      foreach ($modules as $module) {
          try {
              $moduleManager-&gt;loadModule($module-&gt;getName());
              $this-&gt;loadedModules[] = $module-&gt;getName();
          } catch (\Exception $e) {
              $this-&gt;failedModules[] = $module-&gt;getName();
          }
      }
  
      if (count($this-&gt;failedModules) &gt; 0) {
          // Error in loading modules
          exit;
      }
  
      return $this;
  }
  
  public function getServiceConfig()
  {
      return array(
          'factories' =&gt; array(
              'ModuleTable' =&gt;  function($sm) {
                  return new ModuleTable($sm-&gt;get('Zend\Db\Adapter\Adapter'));
              },
          ),
      );
  }
  </code></pre>
  
  <p>what I'm trying to achieve here is to have modules dynamically loaded based on a setting from database.</p>
  
  <p>i get no error in loading modules ... when i try calling back $moduleManager->getLoadedModules(); i see that the module is in the loaded list but its config and its functionality doesnt work. Specifically i have routes in that module and when trying to access them i get 404. but if i include the module in the application.config all works perfect.</p>
  
  <p>Possible to achieve? If yes any guidelines? </p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I managed to get the modules dynamically loaded within the Module::init() method ... but without any success accessing the ServiceManager and/or db access to load the list of modules from db ... </p>