willy68 / pg-router
A fast, flexible, and PSR-7 compatible router for PHP.
v1.0.4
2025-06-04 21:30 UTC
Requires
- php: ~8.1 || ~8.2 || ~8.3 || ~8.4
- fig/http-message-util: 1.1.5
- psr/cache: ^3.0
- psr/container: ^1.0 || ^2.0
- psr/http-message: ^1.0.1
- psr/http-server-middleware: ^1.0
- symfony/cache: ^7.2
Requires (Dev)
- guzzlehttp/psr7: ^2.2
- phpbench/phpbench: ^1.4
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.6
- symfony/var-dumper: ^7.0
- willy68/response-sender: ^1.0
This package is auto-updated.
Last update: 2025-06-08 17:33:15 UTC
README
A fast, flexible, and PSR-7 compatible router for PHP.
Features
- PSR-7 request/response support
- Route grouping and middleware stacking
- Named routes and URL generation
- CRUD route helpers
- Route caching for production
- Extensible matchers and collectors
Installation
composer require pg/router
Basic Usage
use Pg\Router\Router; $router = new Router(); $router->route('/hello/{name: \w+}', function ($request) { $name = $request->getAttribute('name'); (return new Response())->getBody()->write("Hello, $name!"); }, 'hello', ['GET']); $res = $router->match($request); if ($res->isSuccess()) { // add route attributes to the request foreach ($res->getMatchedAttributes() as $key => $val) { $request = $request->withAttribute($key, $val); } $callback = $res->getMatchedRoute()->getCallback(); $response = $callback($request);
Route Groups and Middleware
$router->group('/admin', function ($group) { $group->route('/dashboard', 'AdminController::dashboard', 'admin.dashboard', ['GET']); })->middleware(AdminMiddleware::class);
CRUD Helper
$router->crud('/posts', 'PostController', 'posts');
URL Generation
$url = $router->generateUri('hello', ['name' => 'Alice']);
Testing
This project uses PHPUnit for testing.
./vendor/bin/phpunit
License
MIT License