lukman-ss / router
A lightweight and flexible routing library for PHP 8.2+.
v1.0.1
2026-06-13 08:38 UTC
Requires
- php: >=8.2
- lukman-ss/http: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
Lightweight routing package for PHP 8.2+ built on lukman-ss/http.
Features
- HTTP route registration for
GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD,any(), andmatch(). - Static and dynamic route matching with
where()constraints. - Route groups with prefix, name prefix, and middleware.
- Handler dispatch for closures, array callables, and
Class@method. - Middleware pipeline using
Lukman\Http\MiddlewareInterface. - URL generation for named routes.
Installation
composer require lukman-ss/router
Usage
<?php declare(strict_types=1); use Lukman\Http\Request; use Lukman\Http\Response; use Lukman\Router\Router; $router = new Router(); $router->get('/users/{id}', function (Request $request, string $id): Response { return new Response('User ' . $id); })->where('id', '[0-9]+')->name('users.show'); $response = $router->dispatch(new Request('GET', '/users/42')); echo $response->content();
Groups
<?php declare(strict_types=1); use Lukman\Router\Router; $router = new Router(); $router->group([ 'prefix' => '/api', 'name' => 'api.', 'middleware' => App\Http\Middleware\AuthMiddleware::class, ], function (Router $router): void { $router->get('/users/{id}', App\Http\Controller\UserController::class . '@show') ->name('users.show'); }); $url = $router->url('api.users.show', ['id' => 42, 'tab' => 'profile']); // /api/users/42?tab=profile
Running Tests
composer test
