title: PHP MVC Routing > How can I create a custom route?
body: <p>Im using PHP MVC for my site and I have an issue with routing. </p>
  
  <p>When I go to the index (front page), I use <a href="http://www.example.com" rel="nofollow">http://www.example.com</a> or <a href="http://www.example.com/index" rel="nofollow">http://www.example.com/index</a>. </p>
  
  <p>When I go to the contact page, I use <a href="http://www.example.com/contact" rel="nofollow">http://www.example.com/contact</a>.</p>
  
  <p>When I go to the services or about pages, I use <a href="http://www.example.com/content/page/services" rel="nofollow">http://www.example.com/content/page/services</a> or <a href="http://www.example.com/content/page/about" rel="nofollow">http://www.example.com/content/page/about</a>.</p>
  
  <p>My index and contact pages have their own controllers because they are static pages. But the services and about pages are pulled from my db, thus dynamic. So I created a controller, named it content and just pass the parameters needed to get whatever page I want.</p>
  
  <p>I want to make my URLs more consistent. If I go to the services or about pages, I want to use <a href="http://www.example.com/services" rel="nofollow">http://www.example.com/services</a> or <a href="http://www.example.com/about" rel="nofollow">http://www.example.com/about</a>.</p>
  
  <p>How can I change my routing to meet this requirement? I ultimately would like to be able to create pages in my db, and then pull the page with a URL that looks like it has its own controller. Instead of having to call the content controller to get it to work.</p>
  
  <p>Below are my controllers and what methods they contain, as well as my routing code. </p>
  
  <p><strong>Controllers:</strong></p>
  
  <pre><code>IndexController
    function: index
  
  ContentController
    function: page
    function: sitemap
  
  ContactController
    function: index
    function: process
  </code></pre>
  
  <p><strong>Routing</strong></p>
  
  <pre><code>class Application
  {
  // @var mixed Instance of the controller
  private $controller;
  
  // @var array URL parameters, will be passed to used controller-method
  private $parameters = array();
  
  // @var string Just the name of the controller, useful for checks inside the view ("where am I ?")
  private $controller_name;
  
  // @var string Just the name of the controller's method, useful for checks inside the view ("where am I ?")
  private $action_name;
  
  // Start the application, analyze URL elements, call according controller/method or relocate to fallback location
  public function __construct()
  {
      // Create array with URL parts in $url
      $this-&gt;splitUrl();
  
      // Check for controller: no controller given ? then make controller = default controller (from config)
      if (!$this-&gt;controller_name) {
          $this-&gt;controller_name = Config::get('DEFAULT_CONTROLLER');
      }
  
      // Check for action: no action given ? then make action = default action (from config)
      if (!$this-&gt;action_name OR (strlen($this-&gt;action_name) == 0)) {
          $this-&gt;action_name = Config::get('DEFAULT_ACTION');
      }
  
      // Rename controller name to real controller class/file name ("index" to "IndexController")
      $this-&gt;controller_name = ucwords($this-&gt;controller_name) . 'Controller';
  
      // Check if controller exists
      if (file_exists(Config::get('PATH_CONTROLLER') . $this-&gt;controller_name . '.php')) {
  
          // Load file and create controller
          // example: if controller would be "car", then this line would translate into: $this-&gt;car = new car();
          require Config::get('PATH_CONTROLLER') . $this-&gt;controller_name . '.php';
          $this-&gt;controller = new $this-&gt;controller_name();
  
          // Check for method: does such a method exist in the controller?
          if (method_exists($this-&gt;controller, $this-&gt;action_name)) {
              if (!empty($this-&gt;parameters)) {
                  // Call the method and pass arguments to it
                  call_user_func_array(array($this-&gt;controller, $this-&gt;action_name), $this-&gt;parameters);
              } else {
                  // If no parameters are given, just call the method without parameters, like $this-&gt;index-&gt;index();
                  $this-&gt;controller-&gt;{$this-&gt;action_name}();
              }
          } else {
              header('location: ' . Config::get('URL') . 'error');
          }
      } else {
          header('location: ' . Config::get('URL') . 'error');
      }
  }
  
  // Split URL
  private function splitUrl()
  {
      if (Request::get('url')) {
  
          // Split URL
          $url = trim(Request::get('url'), '/');
          $url = filter_var($url, FILTER_SANITIZE_URL);
          $url = explode('/', $url);
  
          // Put URL parts into according properties
          $this-&gt;controller_name = isset($url[0]) ? $url[0] : null;
          $this-&gt;action_name = isset($url[1]) ? $url[1] : null;
  
          // Remove controller name and action name from the split URL
          unset($url[0], $url[1]);
  
          // rebase array keys and store the URL parameters
          $this-&gt;parameters = array_values($url);
          }
      }
  }
  </code></pre>