title: Routing in RESTful API in PHP
body: <p>I am a bit familiar with PHP MVC. Say, I have a controller like so:</p>
  
  <pre><code>class Customers{
  
      public function method1( param1, param2, param3, ..., param_n ){
  
      }
  }
  </code></pre>
  
  <p>In my bootstraping page, I can grab a browser URL like so:</p>
  
  <pre><code>$url = explode('/', filter_var(rtrim( $_GET['url'], '/' ), FILTER_SANITIZE_URL));
  </code></pre>
  
  <p>I do <code>$controller = $url[0]</code> and <code>$method = $url[1]</code>. Any other elements in <code>$url</code> after the second index are parameters and can be collected into an array variable, say <code>$params</code>. Then I route to the relevant controller method and parameters like so:</p>
  
  <pre><code>call_user_func_array([$controller, $method], $params);
  </code></pre>
  
  <p>PLEASE NOTE: Above code is for illustration purposes. I always do checks in real-life situations. Those checks are not shown here, so do not use the above examples for serious projects.</p>
  
  <p>Now, I want to implement a RESTful API using MVC pattern. What I already know:</p>
  
  <ol>
  <li>No browser is involved, so <code>$_GET['url']</code> is out of it.</li>
  <li>The endpoint is obtained from <code>$_SERVER['REQUEST_URI']</code> </li>
  <li>The method is obtained from <code>$_SERVER['REQUEST_METHOD']</code> </li>
  </ol>
  
  <p>How do I route to an endpoint, for example, <code>customers/{12345}/orders</code> to get the orders of a particular customer with id <code>12345</code>?</p>
  
  <p>How can I do this?</p>