PHPackages                             nih/middleware-dispatcher - 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. nih/middleware-dispatcher

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

nih/middleware-dispatcher
=========================

Fiber-based PSR-15 middleware dispatcher for large or dynamic pipelines, with per-request mutation and lazy PSR-11 resolution.

0.2.0(2mo ago)3143MITPHPPHP 8.4 - 8.5

Since Mar 26Pushed 2mo agoCompare

[ Source](https://github.com/nih-soft/middleware-dispatcher)[ Packagist](https://packagist.org/packages/nih/middleware-dispatcher)[ RSS](/packages/nih-middleware-dispatcher/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (6)Versions (9)Used By (3)

NIH Middleware Dispatcher
=========================

[](#nih-middleware-dispatcher)

Fiber-based PSR-15 middleware dispatcher for large or dynamic middleware pipelines.

Why Another PSR-15 Dispatcher?
------------------------------

[](#why-another-psr-15-dispatcher)

This package is not trying to replace every PSR-15 dispatcher.

Classic PSR-15 dispatchers work well for ordinary static middleware stacks. The problem appears when:

- stack traces grow with middleware depth;
- middleware needs to influence what happens later in the same request.

This package exists to solve exactly those two problems.

It executes the pipeline through `Fiber` and a loop, so stack growth stays under control. It also allows middleware to mutate the remaining pipeline and replace the current request final handler at runtime.

If those problems do not apply to your application, a normal PSR-15 dispatcher is probably enough.

When To Use It
--------------

[](#when-to-use-it)

Use this package when your PSR-15 pipeline is not fully static and middleware needs to influence what happens later in the same request.

Typical cases:

- large middleware pipelines where stack trace size becomes an operational problem;
- route-aware, tenant-aware, or module-aware middleware stacks assembled incrementally;
- feature flags that inject or remove later middleware;
- request-scoped fallback handlers;
- advanced unwind behavior where outer middleware should be skipped.

---

Contents
--------

[](#contents)

- [Installation](#installation)
- [Quick Start](#quick-start)
- [Configuration Before `handle()`](#configuration-before-handle)
- [Before and During `handle()`](#before-and-during-handle)
- [Configuration API](#configuration-api)
- [Dispatch-Time Control](#dispatch-time-control)
- [Testing](#testing)

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

[](#installation)

```
composer require nih/middleware-dispatcher
```

Requires PHP `8.4` or `8.5`.

This package is intentionally framework-agnostic. It depends on PSR interfaces, not on a specific framework.

Quick Start
-----------

[](#quick-start)

Assume `$container` is your PSR-11 container and `$request` is a PSR-7 `ServerRequestInterface` created by your framework or HTTP layer.

Even a small application often has several middleware layers.

```
