title: MVC; Arbitrary routing path levels and parameters
body: <p>I'm working on an (<em>oh no, not another</em>) MVC framework in PHP, primarily for education, but also fun and profit.</p>
  
  <p>Anyways, I'm having some trouble with my <code>Router</code>, specifically routing to the correct paths, with the correct parameters. Right now, I'm looking at a router that (using <code>__autoload()</code>) allows for arbitrarily long routing paths:</p>
  
  <pre><code>"path/to/controller/action"
  "also/a/path/to/a/controller/action"
  </code></pre>
  
  <p>Routing starts at the <code>application</code> directory, and the routing path is essentially parallel with the file system path:</p>
  
  <pre><code>"/framework/application/path/to/controller.class.php" =&gt; "action()"
      class Path_To_Controller{
          public function action(){}
      }
  
  "/framework/application/also/a/path/to/a/controller.class.php" =&gt; "action()"
      class Also_A_Path_To_A_Controller{
          public function action(){}
      }
  </code></pre>
  
  <p>This will allow for module configuration files to be available at varying levels of the application file system. The problem is of course, when we introduce routing path parameters, it becomes difficult differentiating where the routing path ends and the path parameters begin:</p>
  
  <pre><code>"path/to/controller/action/key1/param1/key2/param2"
  </code></pre>
  
  <p>Will obviously be looking for the file:</p>
  
  <pre><code>"/framework/application/path/to/controller/action/key1/param1/key2.class.php"
      =&gt; 'param2()'
  //no class or method by this name can be found
  </code></pre>
  
  <p>This ain't good. Now this smells like a design issue of course, but I'm certain there must be a clean way to circumvent this problem.</p>
  
  <p>My initial thoughts were to test each level of the routing path for directory/file existence.</p>
  
  <ul>
  <li>If it hits 1+ directories followed by a file, additional path components are an action followed by parameters.</li>
  <li>If it hits 1+ directories and no file is found, 404 it.</li>
  </ul>
  
  <p>However, this is still susceptible to erroneously finding files. Sure this can be alleviated by stricter naming conventions and reserving certain words, but I'd like to avoid that if possible.</p>
  
  <p>I don't know if this is the best approach. Has anyone solved such an issue in an elegant manner?</p>