承接 horizom/routing 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

horizom/routing

Composer 安装命令:

composer require horizom/routing

包简介

The Horizom Routing package.

README 文档

README

Total Downloads Latest Stable Version License

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.index
  • posts.create
  • posts.store
  • posts.show
  • posts.edit
  • posts.update
  • posts.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\NotFoundException with code 404
  • Horizom\Routing\Exceptions\MethodNotAllowedException with code 405

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 ResponseInterface return type.
  • Route definitions become immutable once compiled.
  • Middleware entries must be strings or Psr\Http\Server\MiddlewareInterface implementations.

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-10