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->requiresGet();
$response->html('home');
$response->send();
}
public function PageRoute (Request $request, Response $response){
$request->requiresGet();
$response->html('Hi, looking at \"' . $request->parameters["page"]. '\"');
$response->send();
}
public function routes (){
return array(
'/' => 'RootRoute',
'/:page/' => 'PageRoute'
);
}
}
2:
class GeneralRouter extends Router {
public function init (){
$this->get('/', function(Request $req, Response $res){
$response->html('<h1>home</h1>');
$response->send();
});
$this->get('/:page/', function(Request $req, Response $res){
$response->html('<pre>Hi, looking at \"' . $req->parameters["page"]. '\"</pre>');
$response->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 "correct" way of doing it?
routes
vsinit
? you still can pass string as callable in second version and anonymous function in the first \$\endgroup\$