slightly-interactive/router
Composer 安装命令:
composer require slightly-interactive/router
包简介
A PSR-7 and PSR-15 compliant router.
README 文档
README
Overview
Router is a HTTP request router, using PSR-7 requests and responses, and delegating to PSR-15 middlewares and request handlers. It requires a PSR-11 DI container.
Usage
Instantiate the router:
$router = new Router();
Give it a PSR-11 DI container:
$router->setDI($myContainer);
Configure routes:
$router->map('get', '/home', HomeController::class);
You can set individual middleware processors for an individual route:
$router->map('get', '/home', HomeController::class)
->middleware(MyMiddleware::class);
You can also create groups of middleware and add those to routes:
$router->middleware('common')
->add(CommonMiddleware1::class)
->add(CommonMiddleware2::class);
$router->map('get', '/home', HomeController::class)
->middlewareGroup('common')
->middleware(MyMiddleware::class);
The group doesn't have to exist, letting you toggle a set of middleware for a set of routes:
if ($condition) {
$router->middleware('option1')
->add(OptionalMiddleware1::class)
->add(OptionalMiddleware2::class);
}
$router->map('get', '/route1', Route1Controller::class)
->middlewareGroup('option1');
$router->map('get', '/route2', Route2Controller::class)
->middlewareGroup('option2');
The implementation accepts middleware as a shorthand for
middlewareGroup, if and only if the group already exists,
and the DI container has no named instance with that name.
Otherwise, the router can't tell the difference between a
named instance in the DI container and a group name.
This shorthand will be removed in a future version.
You can also set global groups of middleware. These will be run for all requests:
$router->middleware()
->add(GlobalMiddleware1::class)
->add(GlobalMiddleware2::class);
Route Matching Rules
Exact Match
The second parameter of Router::map is a string which is matched against the path part of the request URI.
At its simplest, it can be an exact string match:
/an/example/exact/path/match
This will match the path, regardless of the host, scheme, query string or fragment.
Parameter Match
When matching parameters, we consider the path split into parts
by slashes. Any whole part which starts with { and ends with }
can match any value for the corresponding part of the URI path.
For example:
/entries/{year}/{month}/{day} will match /entries/2021/08/14 and
modify the request to contain attributes:
'year' => '2021'
'month' => '08'
'day' => '14'
It's important to note that only full path parts can be matched, not partial ones. None of the following are valid:
/blah{suffix}
/{prefix}blah
/bl{infix}ah
/{slash/inside}
Multi Parameter Match
Using * as a match part allows for matching of a variable number
of parts of the request URI path.
/blah/* will match /blah/a/b/c and create the following attribute:
'*0' => [ 'a', 'b', 'c' ]
More than one wildcard can be used. They will produce attributes with an incrementing number in their name:
/blah/*/x/* will match /blah/a/b/x/c/d and create the following
attributes:
'*0' => [ 'a', 'b' ]
'*1' => [ 'c', 'd' ]
Single parameters can be mixed with multi parameters. Single can
directly precede multi but not directly follow one, e.g.
/blah/{param1}/*/x/{param2} will match /blah/a/b/c/x/y and create the
attributes:
'param1' => 'a'
'*0' => [ 'b', 'c' ]
'param2' => 'y'
But /blah/*/{param} will never match anything, as the final part
of any URI path will always match the * and never {param}.
slightly-interactive/router 适用场景与选型建议
slightly-interactive/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 611 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 07 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「psr-7」 「psr7」 「PSR-11」 「psr11」 「psr-15」 「psr15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 slightly-interactive/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slightly-interactive/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 slightly-interactive/router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Model of a web-based resource
Facilitates the routine step of bootstrapping PHP applications.
Interfaces for web resource models and services to retrieve and create them
Skolkovo API Client
Interface for a factory to create Psr\Http\Message\StreamInterface objects
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 611
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-20