1
0
Fork
You've already forked router
0
PSR-7/PSR-15 router and dispatcher
  • PHP 100%
Thomas Ernst b115208c3a
All checks were successful
ci / ci (push) Successful in 1m31s
Update README badges
2026年06月10日 21:22:55 +02:00
.forgejo/workflows Allow manual CI runs 2026年06月10日 20:58:19 +02:00
.github Add GitHub mirror README 2026年06月10日 21:14:39 +02:00
docs Update docs tooling 2026年05月12日 15:00:38 +02:00
src The big rename 2026年05月11日 14:07:23 +02:00
tests The big rename 2026年05月11日 14:07:23 +02:00
.editorconfig Sync shared config files 2026年01月30日 22:10:24 +01:00
.gitattributes Update .gitattributes 2026年04月15日 19:36:40 +02:00
.gitignore Update .gitignore 2026年04月15日 17:52:32 +02:00
CHANGELOG.md Update changelog 2026年06月09日 20:19:25 +02:00
composer.json Require dev package v4.2 2026年05月13日 13:12:06 +02:00
LICENSE.md Update license 2026年05月12日 20:13:02 +02:00
mago.toml Update mago config 2026年04月15日 18:38:31 +02:00
phpunit.xml.dist Update coverage config 2026年05月12日 19:55:35 +02:00
psalm.xml.dist Use xml.dist config names 2026年05月11日 14:55:02 +02:00
README.md Update README badges 2026年06月10日 21:22:55 +02:00

Celemas Router

ci code coverage type coverage psalm level Software License

A PSR-7/PSR-15 compatible router and view dispatcher.

Using your PSR-7 request and response factory:

<?php
use Celemas\Router\Dispatcher;
use Celemas\Router\Router;
use Celemas\Router\RoutingHandler;
$router = new Router();
$router->get('/{name}', function (string $name) use ($responseFactory) {
	$response = $responseFactory->createResponse();
	$response->getBody()->write("<h1>{$name}</h1>");
	return $response;
});
$handler = new RoutingHandler($router, new Dispatcher());
$response = $handler->handle($request);

Routes

Define routes directly or inside callback groups. Configure groups only inside the group callback. Groups apply their prefix, name prefix, middleware, Before handlers, After handlers, and controller to every route in the group, even when those settings are declared after the route lines in the callback.

use Celemas\Router\Group;
$router->get('/health', [HealthController::class, 'show'], 'health');
$router->map(['GET', 'POST'], '/login', [AuthController::class, 'login'], 'login');
$router->any('/webhook', $webhook, 'webhook');
$router->group('/albums', function (Group $albums) use ($auth): void {
	$albums->get('', 'index', 'albums.index');
	$albums->get('/{id:\d+}', 'show', 'albums.show');
	$albums->post('', 'create', 'albums.create');
	$albums->middleware($auth);
	$albums->controller(AlbumController::class);
});

Preferred route actions are callables and controller method arrays:

$router->get('/status', fn() => $response);
$router->get('/albums', [AlbumController::class, 'index']);

Inside a controller group, use bare method names:

$router->group('/admin', function (Group $admin): void {
	$admin->controller(AdminController::class);
	$admin->get('', 'index', 'admin.index');
});

Dispatching

RoutingHandler implements PSR-15 RequestHandlerInterface. It matches the request through the router and dispatches the matched route through the dispatcher.

Routing exceptions bubble by default. Catch NotFoundException for 404 responses and MethodNotAllowedException for 405 responses.

Use the low-level match API when you need route inspection before dispatching:

$match = $router->match($request);
$response = (new Dispatcher())->dispatch($request, $match);

Middleware and handlers run in this order:

  1. Dispatcher middleware
  2. Route middleware
  3. View middleware attributes
  4. Before handlers immediately before the view
  5. View execution
  6. After handlers for rendering or response changes

Use PSR middleware for cross-cutting request/response behavior. Use Before for final request changes before the view. Use After to render arbitrary view data or modify a response.

URL generation

Use named routes through the router. Generated URLs include the router prefix and fail fast when path params are missing, unknown, or do not match route constraints.

$router = new Router('/cms');
$router->get('/albums/{id:\d+}', $view, 'albums.show');
$url = $router->url(
	'albums.show',
	['id' => 13],
	query: ['tab' => 'tracks'],
);
// /cms/albums/13?tab=tracks

Static assets stay separate from named routes:

$router->addStatic('/assets', __DIR__ . '/public/assets', 'assets');
$css = $router->asset('assets', 'app.css', bust: true);

License

This project is licensed under the MIT license.