duon/router
最新稳定版本:0.2.0
Composer 安装命令:
composer require duon/router
包简介
PSR-7/PSR-15 router and dispatcher
README 文档
README
A PSR-7/PSR-15 compatible router and view dispatcher.
Using your PSR-7 request and response factory:
<?php use Duon\Router\Dispatcher; use Duon\Router\Router; use Duon\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 Duon\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:
- Dispatcher middleware
- Route middleware
- View middleware attributes
Beforehandlers immediately before the view- View execution
Afterhandlers 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.
统计信息
- 总下载量: 268
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-01