willy68/pg-router

A fast, flexible, and PSR-7 compatible router for PHP.

v1.0.4 2025-06-04 21:30 UTC

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