定制 duon/router 二次开发

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

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

duon/router

最新稳定版本:0.3.0

Composer 安装命令:

composer require duon/router

包简介

PSR-7/PSR-15 router and dispatcher

README 文档

README

ci codecov psalm 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.

duon/router 适用场景与选型建议

duon/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 269 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 duon/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 duon/router 我们能提供哪些服务?
定制开发 / 二次开发

基于 duon/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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