Router Class In a Lightweight MVC Boilerplate I'm trying to come up with a simple router class. I considered using a Request class, but I think that will turn something simple into something overly complex. That is why I did not consider creating one for my framework.

class Router {

    private $uri;
    private $controller;
    private $action;
    private $params;

    public function __construct($uri) {
        $this-&gt;uri = $uri;
        $this-&gt;action = 'index';
        $this-&gt;params = array();
    }

    public function map() {
        $uri = explode('/', $this-&gt;uri);
        if (empty($uri[0])) {
            $c = new Config('app');
            $this-&gt;controller = $c-&gt;default_controller;
        } else {
            $this-&gt;controller = array_shift($uri);
            if (!empty($uri[1]))
                $this-&gt;action = array_shift($uri);
            if(!empty($uri[2]))
                $this-&gt;params = $uri;
        }
    }

    public function getController() {
        return $this-&gt;controller;
    }

    public function getAction() {
        return $this-&gt;action;
    }

    public function getParams() {
        return $this-&gt;params;
    }
}


In the bootstrap, I did this to map the url:

$uri = filter_var(rtrim(isset($_GET['url']) ? $_GET['url'] : null, '/'), FILTER_SANITIZE_URL);
$router = new Router($uri);
$router-&gt;map();


What can I do to improve this?
What is a router used for?

Translate one address to another one connection two different network to eahc other depending on rules. It will never instantita a browser to display for example a HTTP request.

Your class doing to much and much lesser than it should

Your router should only deal with the input address and not instantiating a controller and other stuff. How should it work?


need a collection of rules, call RouteTable (will store Route instances)
rule: how to translate an url to a usefull address in the framwork (how to get named parameters like controller or action and so on)
the Router it self which is containing the logic


Rules, Routes

A rule should contain the following stuffs:


a pattern, say: "{controller}/{action}/{id}"


This will mean that if we have an url like book/details/5 then the framework somewhere will instantiate the book controller and call it's details method with parameter 5.


set up default values are always good practise becouse a route can work without having all parts set


For example an id is not always mandatory it can be optional or we could define a pattern like "veryimportanturl" but the framwork can handle like: controller => lottery, action => getthisweekwinningnumbers, id => 5


a good to have feature is to attach a unique name to our routes so they could be used for generating urls or form action parameters
a powerfull weapon could be if we can say to a route: "hey you have a handler, so if you are used to handle one request then you should tell how can we do that"


We can create different handlers for different routes: can have one for handling an MVC task, another one so serve special images (so we do not need to go through a complete MVC cycle can be create a thinner logic).

These rule should be mapped into our RouteTable which will be the responsible to store these and for example keep the unique route names.

Router

A router will need two things:


the current request data (rewritten url and maybe query string)
the RouteTable


The router is responsible to pick the correct one from the collection can be done different ways but here is my solution:


ordering the available routes: by the static parts (fix parameters not pattern elements ({})) and by the constraints (in a route i can set up constraint like the id onyle can be in the range of 0-5) [static length descending, constraints' numbers descending]
iterating through the ordered rules collection and trying to find out which one will be the first mach by using regular expression (the Route contains it's pattern translated into regular expression), if a match found i try to fill the pattern with data; if everything is in place i've founded otherwise continuing the search.


If the correct route has been found my routing engine returns it to the other parts of the framework but not just the pure route but mapped all the values came from the request. If nothing has found i face with an exception.

The way i see the router above does nearly nothing compared to a real router but do things what should not be done by it. Beside this it's unflexible (fixed array indexes, has a hard dependency on a Config class and a default controller (why?)).