承接 middlewares/base-path-router 相关项目开发

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

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

middlewares/base-path-router

最新稳定版本:v2.1.0

Composer 安装命令:

composer require middlewares/base-path-router

包简介

A PSR-15 middleware for hierarchical dispatching based on path prefixes

README 文档

README

Latest Version on Packagist Software License Testing Total Downloads

A middleware dispatching to other middleware stacks, based on different path prefixes.

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/base-path-router.

composer require middlewares/base-path-router

You may also want to install middlewares/request-handler.

Example

This example uses middleware/request-handler to execute the route handler:

$dispatcher = new Dispatcher([
    new Middlewares\BasePathRouter([
        '/admin' => $admin,
        '/admin/login' => $adminLogin,
        '/blog' => $blog,
    ]),
    new Middlewares\RequestHandler()
]);

$response = $dispatcher->dispatch(new ServerRequest());

BasePathRouter allows anything to be defined as the router handler (a closure, callback, action object, controller class, etc). The middleware will store this handler in a request attribute.

Usage

You have to set an array of paths (as keys) and handlers (as values).

$router = new Middlewares\BasePathRouter([
    '/foo' => $routerFoo,
    '/bar' => $routerBar,
    '/foo/bar' => $routerFooBar,
]);

Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface as the second argument, to create the error responses (404) if the router is not found. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.

$responseFactory = new MyOwnResponseFactory();

$router = new Middlewares\BasePathRouter($paths, $responseFactory);

continueOnError

Set true to continue to the next middleware instead return an empty 404 response for non-matching requests (i.e. those that do not have an URI path start with one of the provided prefixes).

stripPrefix

By default, subsequent middleware will receive a slightly manipulated request object: any matching path prefixes will be stripped from the URI. This helps when you have a hierarchical setup of routers, where subsequent routers (e.g. one for the API stack mounted under the /api endpoint) can ignore the common prefix.

If you want to disable this behavior, use the stripPrefix method:

$router = (new Middlewares\BasePathRouter([
        '/prefix1' => $middleware1,
    ]))->stripPrefix(false);

attribute

The attribute name used to store the handler in the server request. The default attribute name is request-handler.

$dispatcher = new Dispatcher([
    //Save the route handler in an attribute called "route"
    (new Middlewares\BasePathRouter($paths))->attribute('route'),

    //Execute the route handler
    (new Middlewares\RequestHandler())->attribute('route')
]);

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.

统计信息

  • 总下载量: 1.27M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固