i'm trying to create a url router system, for educational purposes, that has a similar usage like Laravel has, but i'm curious about how i can register the routes like laravel does (with single calls to static methods), here's my "conceptual" code that i made using a TDD approach:

        $Route = new Route;
        ->setCallback(function () {
            return 'get user';

        $Route2 = new Route;

        $Router = new Router;
        // if the route is matched this methods are called
        // $Router->handleMiddlewares();
        // $Router->handleCallback();

1 - there's a better approach about how to encapsulate the route object creation to a simple/shorter way? eg: in Laravel we can map routes like this:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback)->name('foo');

2 - should the router class be responsible for filtering routes (regex rules etc), handling callbacks and middlewares?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.