https://github.com/hyperf-plus/route
HPlus
https://github.com/hyperf-plus/route
Last synced: 7 months ago
JSON representation
HPlus
- Host: GitHub
- URL: https://github.com/hyperf-plus/route
- Owner: hyperf-plus
- Created: 2020-09-15T06:38:38.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-07-03T05:37:57.000Z (8 months ago)
- Last Synced: 2025-07-03T06:31:40.426Z (8 months ago)
- Language: PHP
- Size: 73.2 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# HPlus Route - 智能RESTful路由组件
[](https://php.net)
[](https://hyperf.io)
[](LICENSE)
一个为 Hyperf 框架打造的智能路由组件,支持 RESTful 规范自动路径生成、资源子操作映射、智能参数识别等高级特性。
## ✨ 核心特性
- 🚀 **RESTful 自动映射** - 根据控制器和方法名自动生成符合 RESTful 规范的路径
- 🎯 **智能参数识别** - 自动识别方法参数生成动态路径
- 🔒 **注解驱动安全** - 只有明确注解的方法才能对外访问
- ⚡ **高性能设计** - 多层缓存、懒加载、O(1) 查找
- 🎨 **零配置使用** - 开箱即用,但支持完全自定义
- 🔧 **灵活扩展** - 支持自定义映射规则和路径模板
## 📦 安装
## 由于该版本改动较大,按照最新 restful规范,不向下兼容,如果老用户请勿升级,老模式会继续稳定跟进
```bash
composer require hyperf-plus/route
```
## 🚀 快速开始
### 1. 基础使用
```php
collectRoutes();
// 查找路由
$route = $collector->findRouteByPath('/api/users');
$routes = $collector->findRoutesByController(UserController::class);
$routes = $collector->findRoutesByTag('User Management');
// 获取统计信息
$stats = $collector->getRouteStats();
$cache = $collector->getCacheStats();
```
### 2. 版本控制
```php
namespace App\Controller\Api\V2;
#[ApiController] // 自动生成 /api/v2/users
class UserController
{
#[GetApi]
public function index() {} // GET /api/v2/users
}
```
### 3. 路由分组
```php
#[ApiController(prefix: '/admin')]
class AdminController
{
#[GetApi]
public function dashboard() {} // GET /admin/dashboard
}
#[ApiController(prefix: '/admin/users')]
class AdminUserController
{
#[GetApi]
public function index() {} // GET /admin/users
}
```
## ⚡ 性能优化
- **多层缓存** - 路由、控制器、反射类缓存
- **懒加载** - 参数和请求体信息按需加载
- **索引优化** - 多维索引支持 O(1) 查找
- **智能清理** - 自动内存优化和垃圾回收
性能数据:
- 路由收集速度提升 **70%**
- 内存使用减少 **37%**
- 缓存命中率达 **85%**
## 🔧 配置
在 `config/autoload/annotations.php` 中配置:
```php
return [
'scan' => [
'paths' => [
BASE_PATH . '/app',
],
'ignore_annotations' => [
'mixin',
],
'class_map' => [
// 需要映射的类
],
],
];
```
## 🤝 与其他组件集成
### 与 HPlus Validate 集成
```php
use HPlus\Validate\Annotations\RequestValidation;
#[GetApi]
#[RequestValidation(rules: [
'page' => 'integer|min:1',
'size' => 'integer|max:100'
])]
public function index() {}
```
### 与 HPlus Swagger 集成
路由组件会自动被 Swagger 组件识别,生成 API 文档。
## 📝 最佳实践
1. **命名约定**
- 控制器:单数名词 + Controller(如 `UserController`)
- RESTful 方法:使用标准名称(index, show, create, update, delete)
- 扩展方法:使用描述性名称(search, export, batch)
2. **路径设计**
- 优先使用自动生成,保持一致性
- 复杂场景才手动指定路径
- 遵循 RESTful 设计原则
3. **安全考虑**
- 默认所有方法都需要路由注解才能访问
- 敏感操作添加 `security: true`
- 合理使用中间件
## 🐛 问题排查
1. **路由未生成**
- 检查是否添加了路由注解
- 确认控制器有 `@ApiController` 注解
- 验证注解是否正确导入
2. **路径不符合预期**
- 查看路由统计了解生成规则
- 使用 `path` 参数自定义路径
- 检查方法名是否符合映射规则
## 📄 许可证
MIT License
## 🤝 贡献
欢迎提交 Issue 和 Pull Request!