open-core/router
Composer 安装命令:
composer require open-core/router
包简介
PSR-15 compliant fast Router Middleware based on precompiled searching tree
README 文档
README
PSR-15 compliant fast Router Middleware based on precompiled searching tree.
Features
- PSR-7, PSR-11, PSR-15, PSR-17 compliant.
- Controllers with attribute-based (annotations) routing.
- Passing parsed request body as param to controller method.
- Passing PSR request/response objects to controller method.
- Optional controller method params resolved as URL Query params.
- Params and body type casting according handler method signature.
- Router compile advantages:
- No need to load all controller classes every time.
- Because of pre-compiled tree structure, the complexity of matching algorithm is O(log n) instead of O(n).
- Almost no regex matches.
- Reverse routing.
- Fully configurable data deserialization.
Lifecycle
One-time compilation:
- Looking for all Controller and its method according to routes.
- Executing RouteAnnotations to resolve extra Route attributes.
- Building and caching route tree.
- Building and caching data for reverse routing.
Runtime phase 1 (assumption):
- Creating PSR-7
ServerRequestobject.
Runtime phase 2:
- Resolving Controller, method and attributes for PSR-7
ServerRequest. - Modifying request by attaching custom attributes and data required to run controller.
Runtime phase 3 (assumption):
- Running other middlewares, which can be enabled/disabled based on PSR-7
ServerRequestattributes. Example: Authorization or CSFR can be disabled for specific routes.
Runtime phase 4:
- Reading the attributes from PSR-7
ServerRequest. - Running controller with method resolved on Phase 2.
- If controller returned data in simplified form, wrapping it into final response.
Why there is two middlewares for routing?
The main reason is to use annotations (PHP Attributes) to configure other middlewares using Request attributes.
For example, this allows to set AuthMiddleware in between, so it could reject request before it will be handled by Controller.
RouteAnnotations
RouteAnnotations is kind of syntax sugar. The same effect can be reached by Route $attributes parameter.
Because route attributes are resolved before route tree is cache, it should not to be dynamic.
Special handler method arguments
There is ability to pass Request or even parsed body directly to route handler:
- If type of handler method parameter is
ServerRequestInterface, it will be resolved as raw $request. - If type of handler method parameter is
ResponseInterface, it will be resolved as new $response. So no need to callResponseFactoryInterface - If parameter is annotated by
Bodyattribute, it will be resolved bybodyproperty of request. Also it could be parsed as JSON if its type isarray.
Reverse Router
The route path can be generated by route name and params.
open-core/router 适用场景与选型建议
open-core/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 148 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 open-core/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 open-core/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 148
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-15