vagkalosynakis/league-router-attributes
Composer 安装命令:
composer require vagkalosynakis/league-router-attributes
包简介
Attribute-based routing for league/route (PHP 8+)
README 文档
README
Attribute-based routing for league/route. Define routes using PHP 8 attributes directly on controller methods, eliminating manual route registration boilerplate.
Requirements
- PHP ^8.0
- league/route ^5.1
- psr/container ^1.0|^2.0
Installation
composer require vagkalosynakis/league-router-attributes
Usage
Basic Routing
Use the #[Route] attribute to declare routes on controller methods:
<?php namespace App\Controllers; use VagKalosynakis\LeagueRouteAttributes\Attributes\Route; class UserController { #[Route(['GET'], '/users')] public function index(): Response { // List users } #[Route(['GET'], '/users/{id:\d+}')] public function show(int $id): Response { // Show user } #[Route(['POST'], '/users')] public function store(Request $request): Response { // Create user } #[Route(['PUT', 'PATCH'], '/users/{id:\d+}')] public function update(int $id, Request $request): Response { // Update user - handles both PUT and PATCH } #[Route(['DELETE'], '/users/{id:\d+}')] public function destroy(int $id): Response { // Delete user } }
Route Prefixes and Names
#[Route(['GET'], '/admin/users', prefix: '/api/v1', name: 'admin.users')] public function adminUsers(): Response { // Accessible at: /api/v1/admin/users // Named route: admin.users }
Middleware
Apply middleware at class level (inherited by all methods) or method level:
<?php namespace App\Controllers; use VagKalosynakis\LeagueRouteAttributes\Attributes\Route; use VagKalosynakis\LeagueRouteAttributes\Attributes\Middleware; #[Middleware([AuthMiddleware::class])] class AdminController { #[Route(['GET'], '/dashboard')] public function dashboard(): Response { // Inherits AuthMiddleware from class } #[Route(['GET'], '/users')] #[Middleware([AdminOnlyMiddleware::class])] public function users(): Response { // Has both AuthMiddleware and AdminOnlyMiddleware } }
Excluding Middleware
Use #[WithoutMiddleware] to exclude inherited middleware from specific routes:
<?php namespace App\Controllers; use VagKalosynakis\LeagueRouteAttributes\Attributes\Route; use VagKalosynakis\LeagueRouteAttributes\Attributes\Middleware; use VagKalosynakis\LeagueRouteAttributes\Attributes\WithoutMiddleware; #[Middleware([AuthMiddleware::class])] class ApiController { #[Route(['GET'], '/profile')] public function profile(): Response { // Protected: requires AuthMiddleware } #[Route(['GET'], '/health')] #[WithoutMiddleware([AuthMiddleware::class])] public function health(): Response { // Public: AuthMiddleware excluded } }
Route Discovery
Register routes automatically by scanning a directory:
<?php use League\Route\Router; use VagKalosynakis\LeagueRouteAttributes\Discovery\RouteDiscovery; $router = new Router(); $container = /* your PSR-11 container */; $discovery = new RouteDiscovery($router, $container); $discovery->discoverRoutes(__DIR__ . '/src/Controllers'); // Routes are now registered with league/route $response = $router->dispatch($request);
Manual Registration
Or register specific controllers:
<?php $discovery = new RouteDiscovery($router, $container); $discovery->registerRoutesForController(UserController::class); $discovery->registerRoutesForController(AdminController::class);
How It Works
The package uses PHP's Reflection API to scan classes for route attributes, then registers them with league/route using its standard API. All league/route features (middleware, route groups, strategies, etc.) work as expected.
Supported HTTP Methods
GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Multiple HTTP Methods
A single route can handle multiple HTTP methods:
#[Route(['GET', 'HEAD'], '/users')] public function index(): Response { // Responds to both GET and HEAD requests }
Route Parameters
Use league/route's parameter syntax with optional regex constraints:
#[Route(['GET'], '/users/{id:\d+}')] // Numeric ID #[Route(['GET'], '/posts/{slug:[a-z0-9-]+}')] // Alphanumeric slug #[Route(['GET'], '/files/{path:.+}')] // Catch-all path
Architecture
VagKalosynakis\LeagueRouteAttributes\
├── Attributes/
│ ├── Route.php # Route declaration
│ ├── Middleware.php # Middleware application
│ └── WithoutMiddleware.php # Middleware exclusion
└── Discovery/
└── RouteDiscovery.php # Route scanning and registration
PSR Compliance
- PSR-1: Basic Coding Standard
- PSR-4: Autoloading
- PSR-12: Extended Coding Style
- PSR-11: Container Interface
- PSR-15: HTTP Handlers/Middleware
Testing
Running Tests
# Install dev dependencies composer install # Run test suite composer test # Run with coverage composer test:coverage # Static analysis composer phpstan # Code style check composer cs:check # Fix code style composer cs:fix
Test Structure
tests/
├── Unit/ # Unit tests for attributes and discovery
└── Integration/ # Integration tests with league/route
Requirements for Testing
- PHPUnit ^9.6
- Mockery ^1.5
- PHPStan ^1.10
- PHP CS Fixer ^3.16
License
MIT License. See LICENSE file for details.
Contributing
Contributions welcome. Please follow PSR-12 coding standards and ensure tests pass before submitting PRs.
Credits
Built on top of league/route by The PHP League.
vagkalosynakis/league-router-attributes 适用场景与选型建议
vagkalosynakis/league-router-attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「annotations」 「route」 「routing」 「php」 「router」 「attributes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vagkalosynakis/league-router-attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vagkalosynakis/league-router-attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vagkalosynakis/league-router-attributes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A collection of helpers for PHPUnit to ease testing Tarantool libraries.
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
A Laravel-like router for the WordPress Rewrite API
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
A Laravel helper to detect if the current route/path is active.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 39
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-10