title: How to implement a default routing behaviour with Lumen or Slim framework
body: <p>I'm looking into using Lumen or possibly Slim for a project and wondered whether it was possible to autoload controllers based on directory structure rather than having to register all routes.</p>
  
  <p>This is how I would like the autoloading to work .</p>
  
  <p><strong>Example Directory/class structure:</strong></p>
  
  <pre><code> /app/Http/Controllers/
      Foo/
          BarController.php        # App\Http\Controllers\Foo\BarController
  </code></pre>
  
  <p>If the route were </p>
  
  <p><code>example.com/foo/bar</code> == <code>App\Http\Controllers\Foo\BarController::index()</code> </p>
  
  <p><code>example.com/foo/bar/add</code> ==  <code>App\Http\Controllers\Foo\BarController::add()</code></p>
  
  <p>Registered routes should take priority over the autoloaded classes. </p>
  
  <p>I have found a way of doing this based on Opencart's routing method. 
  They have 1145 different public methods over 396 controllers which are all called without explicitly stating the controller &amp; method. 
  Here is my attempt.</p>
  
  <p>.htaccess </p>
  
  <p><code>RewriteRule ^([^?]*) index.php?route=$1 [L,QSA]</code></p>
  
  <p>app/Http/routes.php</p>
  
  <pre><code>$route = array_shift($_GET);
  $method_name = '';
  
  $parts = explode('/', preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route));
  
  while ($parts) {
  
      $class = '\App\Http\Controllers\\' . implode('\\', $parts);
  
      if (class_exists($class)){
          $app-&gt;match($route, $class . '@' . method_exists($class, $method_name) ? $method_name : 'index');
          break;
      } else {
          $method_name = array_pop($parts);
      }
  }
  </code></pre>
  
  <p>If a route is required which differs from the default the Opencart then use <code>.htaccess RewriteRule</code> or a <code>response-&gt;redirect</code> to route to an alternative controller. </p>
  
  <p>I would to use their approach but state my route overrides in 
  <code>app/Http/routes.php</code>. like so </p>
  
  <pre><code>// route overrides 
  $app-&gt;get('/', 'common/home@index');
  $app-&gt;get('/home', 'common/home@index');
  </code></pre>
  
  <p>Am I right in thinking this would make the application run faster as it wouldn't have to search all registered routes for a match ? </p>
  
  <p>Is there a better way of performing this automatic routing process ? </p>