horizom/routing
Composer 安装命令:
composer require horizom/routing
包简介
The Horizom Routing package.
README 文档
README
Lightweight PSR-friendly routing for Horizom applications, built on top of FastRoute.
The package provides:
- HTTP route registration for all common verbs
- Group prefixes, middleware and attributes
- Resource-style route generation
- Redirect and permanent redirect helpers
- PSR-11 container based handler resolution
- Lazy handler resolution for lower bootstrap cost
- PSR-15 compatible request dispatching
Requirements
- PHP 8.0+
- A PSR-11 container
Installation
composer require horizom/routing
Quick Start
Use RouterFactory when you want eager handler resolution, or RouterLazyFactory when handlers should be resolved only when a route is matched.
<?php declare(strict_types=1); use Horizom\Routing\RouterFactory; use Nyholm\Psr7\ServerRequest; use Psr\Container\ContainerInterface; $container = /* PSR-11 container */; $router = (new RouterFactory())->create($container); $router->get('/users/{id}', App\Http\Controllers\UserController::class . '@show'); $request = new ServerRequest('GET', '/users/42'); $response = $router->getRouter()->handle($request);
Registering Routes
The router supports the usual HTTP verbs plus map() and any().
$router->get('/health', App\Http\Controllers\HealthController::class . '@show'); $router->post('/users', App\Http\Controllers\UserController::class . '@store'); $router->map(['GET', 'POST'], '/search', App\Http\Controllers\SearchController::class . '@handle'); $router->any('/webhook', App\Http\Controllers\WebhookController::class . '@handle');
Supported handler styles:
Controller::class . '@method'InvokableController::class[Controller::class, 'method']- Closures returning a
Psr\Http\Message\ResponseInterface
Route Groups
Groups let you share a common prefix and route metadata.
$router->group([ 'prefix' => '/api', 'namespace' => 'App\\Http\\Controllers\\Api\\', 'middleware' => ['auth'], 'attributes' => ['version' => 'v1'], ], function ($router): void { $router->get('/users', 'UserController@index'); $router->get('/users/{id}', 'UserController@show'); });
Group metadata is applied to every route created inside the callback.
Resource Routes
resource() generates the conventional CRUD endpoints for a controller.
$router->resource('/posts', App\Http\Controllers\PostController::class);
Generated route names:
posts.indexposts.createposts.storeposts.showposts.editposts.updateposts.destroy
You can limit the generated endpoints with only or except.
$router->resource('/posts', App\Http\Controllers\PostController::class, [ 'only' => ['index', 'show'], ]); $router->resource('/posts', App\Http\Controllers\PostController::class, [ 'except' => ['destroy'], ]);
To register multiple resources at once:
$router->resources([ '/posts' => App\Http\Controllers\PostController::class, '/comments' => App\Http\Controllers\CommentController::class, ]);
Redirects
$router->redirect('/old-path', '/new-path'); $router->redirectPermanently('/legacy', '/canonical');
Factories
RouterFactory
RouterFactory builds a router with eager handler resolution.
$router = (new RouterFactory())->create($container);
RouterLazyFactory
RouterLazyFactory defers handler resolution until route compilation/matching.
use Horizom\Routing\RouterLazyFactory; $router = (new RouterLazyFactory())->create($container);
Error Handling
RouterHandler throws typed exceptions for common routing failures:
Horizom\Routing\Exceptions\NotFoundExceptionwith code404Horizom\Routing\Exceptions\MethodNotAllowedExceptionwith code405
MethodNotAllowedException::getAllowedMethods() returns the HTTP methods accepted by the matched route.
Testing And Validation
Run the package checks with:
vendor/bin/phpstan analyse src tests --level=9 vendor/bin/phpunit --testdox
The test suite covers route registration, resource filtering, group metadata, redirects, factories, lazy resolution, invocation, dispatching and exception behavior.
Notes
- Route handlers must declare a non-null
ResponseInterfacereturn type. - Route definitions become immutable once compiled.
- Middleware entries must be strings or
Psr\Http\Server\MiddlewareInterfaceimplementations.
License
MIT. See LICENSE.md.
horizom/routing 适用场景与选型建议
horizom/routing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 239 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「php」 「http」 「router」 「horizon」 「horizom」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 horizom/routing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 horizom/routing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 horizom/routing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Write down your routing mapping at one place
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
http客户端
Easily add Excepct-CT header to your project
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
统计信息
- 总下载量: 239
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-10