title: Zend Framework 2 - Dynamically Loading Modules and its configuration
body: <p>I am trying to load modules on the basis of user type in addition to Application module i.e. Application modules is always loaded since it is mentioned in <code>application.config.php</code> file if user is of type 1 I want to load module A, B and D and if user is of type 2, I want to load module C, E and F.</p>
  
  <p>in <code>Module.php</code> of Application module's <code>onBootstrap</code> function I load modules dynamically, and when I see the result <code>var_dump($moduleManager-&gt;loadedModules())</code>, it shows the array of the correct modules that are loaded </p>
  
  <p>But the issue that I am facing is that even though modules are loaded correctly their configuration is not loaded.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>In my module A I have a service called, <code>SomethingService</code> and it is being used in <code>indexAction</code> of <code>IndexController</code> in <code>Application</code> module. But it throws the exception which states</p>
  
  <blockquote>
    <p>Unable to fetch or create instance of SomethingService</p>
  </blockquote>
  
  <p>After some debugging I found that even though the modules are loaded their configuration from <code>module.config.php</code> is not loaded and is not availble in <code>Config</code> service.</p>
  
  <p>To overcome this issue, where I load module, I now get the config using <code>$module-&gt;getConfig()</code> method merge if with the <strong>Config</strong> service and override Config service using the following code</p>
  
  <pre><code>$this-&gt;serviceLocator-&gt;setAllowOverride(true);
  $this-&gt;serviceLocator-&gt;setService('config', $mergedConfig);
  $this-&gt;serviceLocator-&gt;setAllowOverride(false);
  </code></pre>
  
  <p>As a result of this , when I get config using <code>$this-&gt;serviceLocator-&gt;get('config);</code> I see that all the modules config is merged and is available in the <strong>config</strong> array.</p>
  
  <p>After doing all of this, I am still getting the exception that I mentioned above. Maybe I am doing all of this at the wrong location?</p>