Routing System request syntax I'm working on writing a routing system for a PHP project. I'm trying to write it in a way that it can easily be expanded later on via plugins.
I have 2 ideas for how to go about doing it. I've already created working versions of both, now it's just down to which one people find easier to use.
1:
class GeneralRouter extends Router {

    public function RootRoute (Request $request, Response $response){
        $request-&gt;requiresGet();
        $response-&gt;html('home');
        $response-&gt;send();
    }

    public function PageRoute (Request $request, Response $response){
        $request-&gt;requiresGet();
        $response-&gt;html('Hi, looking at \&quot;' . $request-&gt;parameters[&quot;page&quot;]. '\&quot;');
        $response-&gt;send();
    }

    public function routes (){
        return array(
            '/' =&gt; 'RootRoute',
            '/:page/' =&gt; 'PageRoute'
        );
    }
}

2:
class GeneralRouter extends Router {

    public function init (){
        $this-&gt;get('/', function(Request $req, Response $res){
            $response-&gt;html('&lt;h1&gt;home&lt;/h1&gt;');
            $response-&gt;send();
        });

        $this-&gt;get('/:page/', function(Request $req, Response $res){
            $response-&gt;html('&lt;pre&gt;Hi, looking at \&quot;' . $req-&gt;parameters[&quot;page&quot;]. '\&quot;&lt;/pre&gt;');
            $response-&gt;send();
        });
    }
}

#1 is similar to the way I've seen other PHP projects do their routing classes (CraftCMS for example), however I really like the ExpressJS-like format of #2. Which would be easier for expansion and/or is the &quot;correct&quot; way of doing it?
The first example is a violation of the open/closed-principle. You shouldn't need to extend the class in order to change the routes. There doesn't seem to be any other way to use the class without extending it.

class Router {
    protected $routes = [];    

    protected function getRoute(Request $req) {
        // ...
    }

    public function handle(Request $req, Response $res) {
        $fn = $this-&gt;getRoute($req);
        call_user_func($fn, $req, $res);
    }

    public function setRoutes(array $routes)
    {
        $this-&gt;routes = $routes;
    }

    public function addRoute($method, $path, $fn) {
        $this-&gt;routes["$method $path"] = $fn;
    }
}


Instead of setting method as values for the routes, use a callable. This can be a function, method, closure or invokable object.

A user may still choose to extend it and initialize in the constructor if he really wants to.

class GeneralRouter extends Router {

    public function RootRoute (Request $request, Response $response){
       // ...
    }

    public function PageRoute (Request $request, Response $response){
       // ...
    }

    public function __construct() {
        $this-&gt;setRoutes([
            'GET /' =&gt; [self::class, 'RootRoute']
            'GET /:page/' =&gt; [self::class, 'RootRoute']
        ]);
    }
}


Adding a get, post and delete method to the Router class would just add micro framework syntax for those who like it, but it wouldn't fundamentally change the router.



Last, do not invent your own custom Request and Response objects. Use PSR-7 to ensure interoperability with libraries and test suites. There is Zend Diactorors and Jasny HTTP message (developed by me).