PHPackages                             mindplay/middleman - 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. mindplay/middleman

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

mindplay/middleman
==================

PSR-7 middleware dispatcher. Let's stop trying to make this complicated.

4.0.4(1y ago)92533.2k—0.9%10[1 PRs](https://github.com/mindplay-dk/middleman/pulls)8LGPL-3.0+PHPPHP ^7.3 || ^8.0

Since Nov 6Pushed 1y ago6 watchersCompare

[ Source](https://github.com/mindplay-dk/middleman)[ Packagist](https://packagist.org/packages/mindplay/middleman)[ RSS](/packages/mindplay-middleman/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (19)Used By (8)

mindplay/middleman
==================

[](#mindplaymiddleman)

[![PHP Version](https://camo.githubusercontent.com/7049959cf1fbf2161ef99d6472656e53a27f74be9f21fddd6806fb39ef262ecb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e335f2d2d5f382e342532422d626c75652e737667)](https://packagist.org/packages/mindplay/middleman)[![CI](https://github.com/mindplay-dk/middleman/actions/workflows/ci.yml/badge.svg)](https://github.com/mindplay-dk/middleman/actions/workflows/ci.yml)[![Code Coverage](https://camo.githubusercontent.com/b1b28923bf01bfa9ad4f1e3911475aefd3da5568d1d2d96e0d1ed5ea1275c794/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f6d6964646c656d616e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/middleman/?branch=master)

Dead simple PSR-15 / PSR-7 [middleware](#middleware) dispatcher.

Provides (optional) integration with a [variety](https://github.com/container-interop/container-interop#compatible-projects)of dependency injection containers compatible with [PSR-11](https://www.php-fig.org/psr/psr-11/).

To upgrade between major releases, please see [UPGRADING.md](UPGRADING.md).

A growing catalog of PSR-15 middleware-components is available from [github.com/middlewares](https://github.com/middlewares).

Usage
-----

[](#usage)

The constructor expects an array of PSR-15 `MiddlewareInterface` instances:

```
use mindplay\middleman\Dispatcher;

$dispatcher = new Dispatcher([
    new ErrorHandlerMiddleware(...)
    new RouterMiddleware(...),
    new NotFoundMiddleware(...),
]);
```

The `Dispatcher` implements the PSR-15 `RequestHandlerInterface`. This package *only* provides the middleware stack - to run a PSR-15 handler, for example in your `index.php` file, you need a [PSR-15 host](https://packagist.org/packages/mindplay/sapi-host) or a similar facility.

Note that the middleware-stack in the `Dispatcher` is immutable - if you need a stack you can manipulate, `array`, `ArrayObject`, `SplStack` etc. are all fine choices.

### Anonymous Functions as Middleware

[](#anonymous-functions-as-middleware)

You can implement simple middleware "in place" by using anonymous functions in a middleware-stack, using a PSR-7/17 implementation such as [`nyholm/psr7`](https://packagist.org/packages/nyholm/psr7):

```
use Psr\Http\Message\ServerRequestInterface;
use mindplay\middleman\Dispatcher;
use Nyholm\Psr7\Factory\Psr17Factory;

$factory = new Psr17Factory();

$dispatcher = new Dispatcher([
    function (ServerRequestInterface $request, callable $next) {
        return $next($request); // delegate control to next middleware
    },
    function (ServerRequestInterface $request) use ($factory) {
        return $factory->createResponse(200)->withBody(...); // abort middleware stack and return the response
    },
    // ...
]);

$response = $dispatcher->handle($request);
```

### Dependency Injection via the Resolver Function

[](#dependency-injection-via-the-resolver-function)

If you want to integrate with an [IOC container](https://github.com/container-interop/container-interop#compatible-projects)you can use the `ContainerResolver` - a "resolver" is a callable which gets applied to every element in your middleware stack, with a signature like:

```
function (string $name) : MiddlewareInterface

```

The following example obtains middleware components on-the-fly from a DI container:

```
$dispatcher = new Dispatcher(
    [
        RouterMiddleware::class,
        ErrorMiddleware::class,
    ],
    new ContainerResolver($container)
);
```

If you want the `Dispatcher` to integrate deeply with your framework of choice, you can implement this as a class implementing the magic `__invoke()` function (as `ContainerResolver` does) - or "in place", as an anonymous function with a matching signature.

If you want to understand precisely how this component works, the whole thing is [just one class with a few lines of code](src/Dispatcher.php) - if you're going to base your next project on middleware, you can (and should) understand the whole mechanism.

Middleware?
-----------

[](#middleware)

Middleware is a powerful, yet simple control facility.

If you're new to the concept of middleware, the following section will provide a basic overview.

In a nutshell, a middleware component is a function (or [MiddlewareInterface](src/MiddlewareInterface.php) instance) that takes an incoming (PSR-7) `RequestInterface` object, and returns a `ResponseInterface` object.

It does this in one of three ways: by *assuming*, *delegating*, or *sharing* responsibility for the creation of a response object.

#### 1. Assuming Responsibility

[](#1-assuming-responsibility)

A middleware component *assumes* responsibility by creating and returning a response object, rather than delegating to the next middleware on the stack:

```
use Zend\Diactoros\Response;

function ($request, $next) {
    return (new Response())->withBody(...); // next middleware won't be run
}
```

Middleware near the top of the stack has the power to completely bypass middleware further down the stack.

#### 2. Delegating Responsibility

[](#2-delegating-responsibility)

By calling `$next`, middleware near the top of the stack may choose to fully delegate the responsibility for the creation of a response to other middleware components further down the stack:

```
function ($request, $next) {
    if ($request->getMethod() !== 'POST') {
        return $next($request); // run the next middleware
    } else {
        // ...
    }
}
```

Note that exhausting the middleware stack will result in an exception - it's assumed that the last middleware component on the stack always produces a response of some sort, typically a "404 not found" error page.

#### 3. Sharing Responsibility

[](#3-sharing-responsibility)

Middleware near the top of the stack may choose to delegate responsibility for the creation of the response to middleware further down the stack, and then make additional changes to the returned response before returning it:

```
function ($request, $next) {
    $result = $next($request); // run the next middleware

    return $result->withHeader(...); // then modify it's response
}
```

The middleware component at the top of the stack ultimately has the most control, as it may override any properties of the response object before returning.

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 94.1% of commits — single point of failure

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 ~230 days

Recently: every ~320 days

Total

15

Last Release

627d ago

Major Versions

1.1.0 → 2.0.02016-09-29

2.0.1 → 3.0.02017-11-29

3.1.0 → 4.0.02021-02-27

PHP version history (3 changes)1.0.0PHP &gt;=5.4

3.0.0PHP &gt;=7.0

3.0.4PHP ^7.3 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9445f567f43ee7a963270651e40e533634586f959e4df3d5398d001b1cb49be8?d=identicon)[mindplay.dk](/maintainers/mindplay.dk)

---

Top Contributors

[![mindplay-dk](https://avatars.githubusercontent.com/u/103348?v=4)](https://github.com/mindplay-dk "mindplay-dk (64 commits)")[![fetchandadd](https://avatars.githubusercontent.com/u/15845152?v=4)](https://github.com/fetchandadd "fetchandadd (2 commits)")[![Brammm](https://avatars.githubusercontent.com/u/851445?v=4)](https://github.com/Brammm "Brammm (1 commits)")[![hannesvdvreken](https://avatars.githubusercontent.com/u/1410358?v=4)](https://github.com/hannesvdvreken "hannesvdvreken (1 commits)")

---

Tags

dispatchermiddlewarephp7psr-15

### Embed Badge

![Health badge](/badges/mindplay-middleman/health.svg)

```
[![Health](https://phpackages.com/badges/mindplay-middleman/health.svg)](https://phpackages.com/packages/mindplay-middleman)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[thecodingmachine/graphqlite

Write your GraphQL queries in simple to write controllers (using webonyx/graphql-php).

5723.1M30](/packages/thecodingmachine-graphqlite)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)[windwalker/framework

The next generation PHP framework.

25639.1k1](/packages/windwalker-framework)

PHPackages © 2026

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