title: How to structure a modular app in Laravel 5?
body: <p>I would like to divide my application in modules. For instance, there would be a "core" modules that contains the basic login functionality, app layout/formatting (CSS etc), user management and a diary.</p>
  
  <p>Later on I may create other modules like a contact manager that can easily be added or removed from the application.</p>
  
  <p>There would be some logic in the apps navigation for determining which modules are present and to show/hide the links to them.</p>
  
  <p>How can I do this in terms of directory structure, namespaces and anything else that's needed?</p>
  
  <hr>
  
  <p>I am looking at creolab/laravel-modules but it states that it is for Laravel 4. Can I still use it with 5 in exactly the same way?</p>
  
  <p>The documentation says to place models, controllers and views within each module directory, but how does this work with routes? Ideally I would like each module to have its own routes.php file. How will all of this work with the stuff in the <code>http</code> and the <code>resources</code> directory?</p>
  
  <hr>
  
  <p>I was thinking of something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/tGpY4.png" alt="Module idea"></p>
  
  <p>But I have no idea how I would get it to work.</p>
  
  <hr>
  
  <p>I have just tried the tutorial here:</p>
  
  <p><a href="http://creolab.hr/2013/05/modules-in-laravel-4/">http://creolab.hr/2013/05/modules-in-laravel-4/</a></p>
  
  <p>With no extra libraries etc, just pure Laravel 5.</p>
  
  <p>I seem to have hit a brick wall with an error message:</p>
  
  <pre><code>FatalErrorException in ServiceProvider.php line 16:
  Call to undefined method Illuminate\Config\Repository::package()
  </code></pre>
  
  <p>Regarding the following:</p>
  
  <pre><code>&lt;?php namespace App\Modules;
  
  abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
  {
  
      public function boot()
      {
          if ($module = $this-&gt;getModule(func_get_args())) {
              $this-&gt;package('app/' . $module, $module, app_path() . '/modules/' . $module);
          }
      }
  
      public function register()
      {
          if ($module = $this-&gt;getModule(func_get_args())) {
              $this-&gt;app['config']-&gt;package('app/' . $module, app_path() . '/modules/' . $module . '/config');
  
  // Add routes
              $routes = app_path() . '/modules/' . $module . '/routes.php';
              if (file_exists($routes)) require $routes;
          }
      }
  
      public function getModule($args)
      {
          $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;
  
          return $module;
      }
  
  }
  </code></pre>
  
  <p>What is causing this and how can I fix it?</p>
  
  <hr>
  
  <p>Got my head around this a bit more now. Got my package/module routes and views working which is great:</p>
  
  <p>
  
  <pre><code>abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
  {
  
      public function boot()
      {
          if ($module = $this-&gt;getModule(func_get_args())) {
              include __DIR__.'/'.$module.'/routes.php';
          }
          $this-&gt;loadViewsFrom(__DIR__.'/'.$module.'/Views', 'core');
      }
  
      public function register()
      {
          if ($module = $this-&gt;getModule(func_get_args())) {
  
          }
      }
  
      public function getModule($args)
      {
          $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;
  
          return $module;
      }
  
  }
  </code></pre>
  
  <p>I have one last question, how would I load all my controllers from inside my package, much like how the <code>loadViewsFrom()</code> method works?</p>