定制 duon/router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

duon/router

最新稳定版本:0.2.0

Composer 安装命令:

composer require duon/router

包简介

PSR-7/PSR-15 router and dispatcher

README 文档

README

Software License Codacy Badge Codacy Badge Psalm level Psalm coverage

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:

  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.

统计信息

  • 总下载量: 268
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固