httpsoft/http-router
Composer 安装命令:
composer require httpsoft/http-router
包简介
Simple and fast HTTP request router providing PSR-7 and PSR-15
README 文档
README
This package provides convenient management of HTTP request routing with support for PSR-7 and PSR-15.
Documentation
Installation
This package requires PHP version 7.4 or later.
composer require httpsoft/http-router
Usage
use HttpSoft\Router\RouteCollector; /** * @var mixed $handler */ $router = new RouteCollector(); // Defining routes. $router->get('home', '/', $handler); $router->post('logout', '/logout', $handler); $router->add('login', '/login', $handler, ['GET', 'POST']); // Custom regular expressions for placeholder parameter tokens. $router->delete('post.delete', '/post/delete/{id}', $handler)->tokens(['id' => '\d+']); // Generate path '/post/delete/25' $router->routes()->path('post.delete', ['id' => 25]); // Generate url '//example.com/post/delete/25' $router->routes()->url('post.delete', ['id' => 25], 'example.com'); // Generate url 'https://example.com/post/delete/25' $router->routes()->url('post.delete', ['id' => 25], 'example.com', true);
Set the parameter to the default value.
$router->get('post.view', '/post/{slug}{format}', $handler) ->tokens(['slug' => '[\w\-]+', 'format' => '\.[a-zA-z]{3,}']) ->defaults(['format' => '.html']) ; // Generate path '/post/post-slug.html'. $router->routes()->path('post.view', ['slug' => 'post-slug']);
Tokens of the route enclosed in [...] are considered optional.
$router->get('post.list', '/posts{[page]}', $handler) ->tokens(['page' => '\d+']) ; // '/posts/33' $router->routes()->path('post.list', ['page' => 33]); // '/posts' $router->routes()->path('post.list');
If necessary, you can specify a specific host for route matching.
// Only for example.com $router->get('page', '/page', $handler) ->host('example.com') ; // Only for subdomain.example.com $router->get('page', '/page', $handler) ->host('subdomain.example.com') ; // Only for shop.example.com or blog.example.com $router->get('page', '/page', $handler) ->host('(shop|blog).example.com') ;
You can specify routes inside of a group.
$router->group('/post', static function (RouteCollector $router): void { // '/post/post-slug' $router->get('post.view', '/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']); // '/post' or '/post/2' $router->get('post.list', '/list{[page]}', ListHandler::class)->tokens(['page' => '\d+']); }); // The result will be equivalent to: $router->get('post.view', '/post/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']); $router->get('post.list', '/post/list{[page]}', ListHandler::class)->tokens(['page' => '\d+']);
Check matching routes.
/** * @var mixed $handler * @var Psr\Http\Message\UriInterface $uri * @var Psr\Http\Message\ServerRequestInterface $request */ $router->get('page', '/page/{id}', $handler)->tokens(['id' => '\d+']); // Match $route = $router->routes()->match($request->withUri($uri->withPath('/page/11'))); $route->getMatchedParameters(); // ['id' => '11'] // Mismatch $router->routes()->match($request->withUri($uri->withPath('/page/slug'))); // null
httpsoft/http-router 适用场景与选型建议
httpsoft/http-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43.52k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「php」 「http」 「router」 「psr-7」 「http-router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 httpsoft/http-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 httpsoft/http-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 httpsoft/http-router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
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
A Laravel helper to detect if the current route/path is active.
http客户端
统计信息
- 总下载量: 43.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-25