nih/router
Composer 安装命令:
composer require nih/router
包简介
Tree-based PHP router for runtime-built route configuration, convention-based matching, URL generation, and PSR-15 middleware integration.
关键字:
README 文档
README
Tree-based, site-aware router configuration for PHP with convention-based matching, optional PSR-15 dispatch, and URL generation.
Why Another Router
Routers like Symfony Router are excellent, but they are optimized around a compiled representation. If route configuration must be assembled dynamically at runtime and cannot be persisted, a classic short-lived PHP request pays that build cost again on every request.
There is another practical issue: in many popular routers every route is described explicitly, one by one. On large projects that gets repetitive fast. When whole branches follow predictable naming, a convention-based rule can be much easier to maintain than a long flat list of route declarations.
This package targets a different space:
- no separate compile step
- runtime-built configuration when needed
- tree/group-oriented configuration instead of route-by-route registration
- convention-based matching and generation for large route subtrees
- middleware scopes attached to any branch
What It Is
- a tree-based configuration API centered around branches, not a flat explicit route registry
- a core runtime made of
RouteMatcherandUrlGenerator - an optional PSR-15 integration layer built on the same config tree
When To Use It
This package fits best when:
- different modules need to extend the same path subtree independently
- large route branches follow naming conventions
- you want the same configuration to drive matching, generation, and dispatch
Installation
composer require nih/router
Minimal Example
use NIH\Router\RouteMatcher; use NIH\Router\RouterConfig; use NIH\Router\Strategy\PathToClassStrategy; use NIH\Router\UrlGenerator; $config = new RouterConfig(); $config->site('https://example.com'); $config->path('/health') ->action('', App\Controller\HealthAction::class, '__invoke', ['GET']); $config->path('/forums/') ->strategy(PathToClassStrategy::class, [ 'namespace' => 'App\\Controller\\Forums', ]); $matcher = new RouteMatcher($config); $generator = new UrlGenerator($config); $health = $matcher->match('/health', 'GET'); $forum = $matcher->match('/forums/view', 'GET'); $path = $generator->generatePath('/forums/view'); $url = $generator->generateUrl('/forums/view', ['page' => 2]);
How to read this config:
path('/health')->action(...)adds one exact terminal routepath('/forums/')->strategy(...)attaches a rule to the whole/forums/subtreePathToClassStrategyresolves controller classes by path and HTTP method convention
Typical PathToClassStrategy mapping for namespace App\\Controller\\Forums:
GET /forums/->App\\Controller\\Forums\\GetGET /forums/view->App\\Controller\\Forums\\ViewGet- matched method is always
__invoke
Core Runtime API
RouteMatcher::match() returns a RouteMatchResult object with:
statusclassmethodrouteParamsqueryParamsallowedMethodsmiddlewares
Status constants live on RouteMatcher:
RouteMatcher::FOUNDRouteMatcher::NOT_FOUNDRouteMatcher::METHOD_NOT_ALLOWED
Important Notes
generatePath()andgenerateUrl()work with the logical configured path. Consumer strategies may fill real URL segments from params during generation.- Consumer strategies may consume either the whole current remainder path, such as
PathTemplateConsumer, or just one next segment, such as the segment consumers. - Configured prefixes, runtime paths, and runtime sites are lowercased immediately.
- Runtime matching does not collapse repeated slashes or rewrite backslashes.
- The core router works with strings and arrays. PSR-7 and PSR-15 live in the middleware layer.
Documentation
- docs/configuration.md - configuration model,
path(),action(), strategies, middlewares, sites, aliases - docs/router.md -
RouteMatcher,UrlGenerator, runtime rules,RouteMatchResult - docs/psr15-integration.md -
RouteMatchMiddleware,RouteDispatchMiddleware, dispatch attributes
nih/router 适用场景与选型建议
nih/router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「router」 「middleware」 「psr-7」 「psr-15」 「php-router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nih/router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nih/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nih/router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
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.
Router
Slim Framework 3 CSRF protection middleware utilities
PMVC App Action Router
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 33
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-27