nih/router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 RouteMatcher and UrlGenerator
  • 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 route
  • path('/forums/')->strategy(...) attaches a rule to the whole /forums/ subtree
  • PathToClassStrategy resolves controller classes by path and HTTP method convention

Typical PathToClassStrategy mapping for namespace App\\Controller\\Forums:

  • GET /forums/ -> App\\Controller\\Forums\\Get
  • GET /forums/view -> App\\Controller\\Forums\\ViewGet
  • matched method is always __invoke

Core Runtime API

RouteMatcher::match() returns a RouteMatchResult object with:

  • status
  • class
  • method
  • routeParams
  • queryParams
  • allowedMethods
  • middlewares

Status constants live on RouteMatcher:

  • RouteMatcher::FOUND
  • RouteMatcher::NOT_FOUND
  • RouteMatcher::METHOD_NOT_ALLOWED

Important Notes

  • generatePath() and generateUrl() 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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 nih/router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 33
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-27