free-elephants/psr-router 问题修复 & 功能扩展

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

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

free-elephants/psr-router

最新稳定版本:0.0.6

Composer 安装命令:

composer require free-elephants/psr-router

包简介

Framework Agnostic PSR Router

README 文档

README

Usage

$routes = [
    // 1. "flat" syntax - single level configuration
    '/api/v1/users' => GetUsersHandler::class, // by default GET method ar user for single value
    // 2. Method-based syntax - two levels configuration
    '/api/v1/users/{id}' => [ // Router parse path params and set named arguments to request (method based syntax
        'GET' => GetUserHandler::class,
        'PATCH' => UpdateUserHandler::class,
        'DELETE' => new class implements \Psr\Http\Server\RequestHandlerInterface {
            // psr handler instance allowed as value too 
        }
    ],
];

$fastRouteDispatcher = (new \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder())
    ->addConfig($routes)
    ->addRoute('/foo', FooHandler::class) // You can define routes by one
    ->build(); 

$router = new \FreeElephants\PsrRouter\Router(
    $fastRouteDispatcher,
    new \FreeElephants\PsrRouter\RequestHandlerFactory($psrContainer),
);

Customization

Slash Normalizing

For handle trailing slashes in path you can inject PathNormalizer implementation into both classes:

  • Router - its rtrim trailing slash at runtime from request path.
  • DispatcherBuilder - its rtrim on add route at build time.
$pathNormalizer = new \FreeElephants\PsrRouter\PathNormalization\TrailingSlashTrimmer();
$dispatcher = (new \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder(
    pathNormalizer: $pathNormalizer))
    ...
    ...
    ->build();


$router = new \FreeElephants\PsrRouter\Router(
    $dispatcher,
    $requestHandlerFactory,
    pathNormalizer: $pathNormalizer
    
)

OPTIONS Handling

By default, Router does not know about OPTIONS and allowed methods for route.

For handle OPTIONS request, you can set your own handler prototype. Every route will be handling this method according to other collected methods.

/**
 * @var \FreeElephants\PsrRouter\FastRoute\DispatcherBuilder $dispatcherBuilder
 */
$dispatcherBuilder->setOptionsHandlerPrototype($optionsRequestHandlerImpl)->build();

Middleware Configuration Support

$middleware = (new \FreeElephants\Middleware\Laminas\MiddlewareBuilder($container))
    ->setDefaultMethods(['GET', 'POST']) // see MiddlewareBuilder::DEFAULT_METHODS
    ->addConfig([
        // Examples for different levels of configuration, anywhere path based
        '/' => RootMiddlewareForAllDefaultMethods::class,
        '/foo' => [
            FooMiddlewareA::class,
            FooMiddlewareB::class,
        ], 
        '/foo/{arg}' => [
            'PUT' => [
                PutFooMiddleware::class,
            ],
            'OPTIONS' => FooWithArgOptionsMiddleware::class,
        ],   
    ])
    ->build();

$middleware->process($request, $routerOrYourOwnedRequestHandler);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2025-06-11