PHPackages                             middlewares/base-path-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. middlewares/base-path-router

ActiveLibrary[HTTP &amp; Networking](/categories/http)

middlewares/base-path-router
============================

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

v2.1.0(1y ago)41.4M↓21%5MITPHPPHP ^7.2 || ^8.0CI passing

Since Aug 22Pushed 1y ago2 watchersCompare

[ Source](https://github.com/middlewares/base-path-router)[ Packagist](https://packagist.org/packages/middlewares/base-path-router)[ Docs](https://github.com/middlewares/base-path-router)[ RSS](/packages/middlewares-base-path-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (8)Versions (9)Used By (5)

middlewares/base-path-router
============================

[](#middlewaresbase-path-router)

[![Latest Version on Packagist](https://camo.githubusercontent.com/abb158bf155dd5881b0ece3f356f9e63fb2b6dd1c636ada4a8010d0e64ca1f26/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f626173652d706174682d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/base-path-router)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/base-path-router/workflows/testing/badge.svg)](https://github.com/middlewares/base-path-router/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/5d4adab4a46d8467b80de044611c78d57ba791504e139c74b7a60762ea5bb6e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f626173652d706174682d726f757465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/base-path-router)

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

Requirements
------------

[](#requirements)

- PHP &gt;= 7.2
- A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)

Installation
------------

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/base-path-router](https://packagist.org/packages/middlewares/base-path-router).

```
composer require middlewares/base-path-router
```

You may also want to install [middlewares/request-handler](https://packagist.org/packages/middlewares/request-handler).

Example
-------

[](#example)

This example uses [middleware/request-handler](https://github.com/middlewares/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
-----

[](#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](https://github.com/middlewares/utils#factory) will be used to detect it automatically.

```
$responseFactory = new MyOwnResponseFactory();

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

### continueOnError

[](#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

[](#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

[](#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](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

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

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance45

Moderate activity, may be stable

Popularity43

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~343 days

Recently: every ~593 days

Total

8

Last Release

422d ago

Major Versions

v0.2.1 → v1.0.02018-09-23

v1.0.0 → v2.0.02019-12-03

PHP version history (3 changes)v0.1.0PHP ^7.0

v2.0.0PHP ^7.2

v2.0.1PHP ^7.2 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e99df56b617f4af8cf0556a51a0ca20c7420104920c57d7e9eab093f92bb744f?d=identicon)[oscarotero](/maintainers/oscarotero)

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

![](https://www.gravatar.com/avatar/8a8620604a1f4094fee4ab1c3d2f6fccb6583293daf5f866c804d155c05b2e13?d=identicon)[franzliedke](/maintainers/franzliedke)

---

Top Contributors

[![franzliedke](https://avatars.githubusercontent.com/u/249125?v=4)](https://github.com/franzliedke "franzliedke (16 commits)")[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (16 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (5 commits)")[![coisa](https://avatars.githubusercontent.com/u/426835?v=4)](https://github.com/coisa "coisa (1 commits)")

---

Tags

middlewarepathpsr-15routerhttppsr-7middlewareserverroutingpsr-15

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/middlewares-base-path-router/health.svg)

```
[![Health](https://phpackages.com/badges/middlewares-base-path-router/health.svg)](https://phpackages.com/packages/middlewares-base-path-router)
```

###  Alternatives

[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[middlewares/negotiation

Middleware to implement content negotiation

47442.1k11](/packages/middlewares-negotiation)[middlewares/payload

Middleware to parse the body of the request with support for json, csv and url-encode

32466.8k17](/packages/middlewares-payload)[middlewares/http-authentication

Middleware to implement Basic and Digest Http authentication

35302.0k2](/packages/middlewares-http-authentication)[middlewares/whoops

Middleware to use Whoops as error handler

33205.4k24](/packages/middlewares-whoops)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
