承接 vagkalosynakis/league-router-attributes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

vagkalosynakis/league-router-attributes

Composer 安装命令:

composer require vagkalosynakis/league-router-attributes

包简介

Attribute-based routing for league/route (PHP 8+)

README 文档

README

PHP Version League Route License

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-10