debuss-a/fastroute-attribute-loader
Composer 安装命令:
composer require debuss-a/fastroute-attribute-loader
包简介
An attribute route loader for FastRoute.
README 文档
README
A simple and elegant route loader for nikic/fast-route that uses PHP 8 attributes to define routes directly on your controller methods.
Installation
Install via Composer:
composer require debuss-a/fastroute-attribute-loader
Requirements
- PHP 8.2 or higher
- nikic/fast-route ^1.0
Usage
1. Define Routes with Attributes
Use the #[Route] attribute on your controller methods:
<?php namespace App\Controller; use FastRoute\Attribute\Route; use Psr\Http\Message\{ResponseInterface,ServerRequestInterface}; use Psr\Http\Server\RequestHandlerInterface; class UserController implements RequestHandlerInterface { #[Route('/users[/{id:\d+}]', methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])] public function handle(ServerRequestInterface $request): ResponseInterface { $id = $request->getAttribute('id'); $method = $request->getMethod(); $body = $request->getParsedBody(); // Handle the request based on $method and $id return match ($method) { 'GET' => $this->getUser($id), 'POST' => $this->createUser($body), 'PUT', 'PATCH' => $this->updateUser($id, $body), 'DELETE' => $this->deleteAlbum($id), }; } public function getUser(?int $id = null): ResponseInterface { // List all users or return a specific user if $id is provided } public function createUser(array $body): ResponseInterface { // Create a new user } public function updateUser(int $id, array $body): ResponseInterface { // Update a user } public function deleteUser(int $id): ResponseInterface { // Delete a user } }
2. Load Routes and Create Dispatcher
<?php use FastRoute\Attribute\AttributeRouteLoader; use FastRoute\RouteCollector; use function FastRoute\simpleDispatcher; // Create the route loader with your namespace and controllers directory $loader = new AttributeRouteLoader( namespace: 'App\\Controller', path: __DIR__ . '/src/Controller' ); // Create the dispatcher $dispatcher = simpleDispatcher(function(RouteCollector $collector) use ($loader) { $loader->load($collector); }); // Dispatch the request $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; // Strip query string (?foo=bar) and decode URI if (false !== $pos = strpos($uri, '?')) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case \FastRoute\Dispatcher::NOT_FOUND: // ... 404 Not Found break; case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; // ... 405 Method Not Allowed break; case \FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; // ... call $handler with $vars break; }
Route Attribute
The Route attribute accepts the following parameters:
path(string, required): The route pattern (supports FastRoute syntax)methods(array, optional): HTTP methods (defaults to['GET'])
Examples
// Simple GET route #[Route('/')] // Route with specific HTTP method #[Route('/users', methods: ['POST'])] // Route with multiple HTTP methods #[Route('/users/{id:\d+}', methods: ['PUT', 'PATCH'])] // Route with optional parameter #[Route('/posts[/{id:\d+}]')] // Multiple routes on the same method #[Route('/road-one')] #[Route('/road-two')] public function handle(ServerRequestInterface $request): ResponseInterface { // Handle both routes }
Group Attribute
The Group attribute accepts the following parameter:
path(string, required): The base route pattern (supports FastRoute syntax)
This path will be prefixed to all routes defined within the class.
A Controller attribute is also provided for semantic purposes, which extends the Group attribute.
Examples
<?php namespace Tests\Controller; use FastRoute\Attribute\{Group, Route}; use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; use Psr\Http\Server\RequestHandlerInterface; use RuntimeException; #[Group('/grouped/v1')] class GroupedController implements RequestHandlerInterface { #[Route('/endpoint')] public function handle(ServerRequestInterface $request): ResponseInterface { // Will be called for GET /grouped/v1/endpoint throw new RuntimeException('Not implemented'); } }
License
MIT License. See LICENSE.md for more information.
debuss-a/fastroute-attribute-loader 适用场景与选型建议
debuss-a/fastroute-attribute-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「router」 「attributes」 「FastRoute」 「fast-route」 「psr-15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 debuss-a/fastroute-attribute-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 debuss-a/fastroute-attribute-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 debuss-a/fastroute-attribute-loader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
A Laravel-like router for the WordPress Rewrite API
A Laravel helper to detect if the current route/path is active.
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-31