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

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

rapp-lib/middleman
==================

PSR-7 middleware dispatcher. Let's stop trying to make this complicated. | mindplay/middleman PHP5.3 compatibilty fork

2.0.1.1(8y ago)0234LGPL-3.0+PHPPHP &gt;=5.3

Since Jun 22Pushed 4y ago1 watchersCompare

[ Source](https://github.com/rapp-lib/middleman)[ Packagist](https://packagist.org/packages/rapp-lib/middleman)[ RSS](/packages/rapp-lib-middleman/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

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

[](#mindplaymiddleman)

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 compliant with [container-interop](https://github.com/container-interop/container-interop).

To upgrade from 1.x to 2.x, please see [UPGRADING.md](UPGRADING.md).

[![PHP Version](https://camo.githubusercontent.com/581d3b86ad2a67effa63792b85b18395acbbc26aae6ab4a453ed555677b8761c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d352e342532422d626c75652e737667)](https://packagist.org/packages/mindplay/middleman)[![Build Status](https://camo.githubusercontent.com/4fbbffe547a0e606dca7639943c5cacc8bd8f1795e3ac2aeaf436a1fc29c29ac/68747470733a2f2f7472617669732d63692e6f72672f6d696e64706c61792d646b2f6d6964646c656d616e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mindplay-dk/middleman)[![Code Coverage](https://camo.githubusercontent.com/b1b28923bf01bfa9ad4f1e3911475aefd3da5568d1d2d96e0d1ed5ea1275c794/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f6d6964646c656d616e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/middleman/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dbb3a34318efe72c7189dce59a58150d198e6edcfe3f0262867dc44c906e69e9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f6d6964646c656d616e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/middleman/?branch=master)

Let's stop trying to make this complicated:

```
use Psr\Http\Message\RequestInterface as Request;
use Zend\Diactoros\Response;

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

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

For simplicity, the middleware stack itself is immutable - if you need a stack you can manipulate, `array`, `ArrayObject`, `SplStack` etc. are all fine choices.

If you prefer implementing middleware as a reusable class, just implement `__invoke()` with the correct callback signature - or, optionally, implement [MiddlewareInterface](src/MiddlewareInterface.php), like this:

```
use Psr\Http\Message\RequestInterface as Request;

class MyMiddleware implements MiddlewareInteface
{
    public function __invoke(Request $request, callable $next) {
        // ...
    }
}
```

Note that this works with or without `implements MiddlewareInterface`, as long as you get the callback signature right.

If you want to wire it to a [DI container](https://github.com/container-interop/container-interop#compatible-projects)you can add a "resolver", which gets applied to every element in your middleware stack - for example:

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

Note that the "resolver" is any callable with a signature like `function (string $name) : MiddlewareInterface` - if you want the `Dispatcher` to integrate deeply with your framework of choice, you can use a custom resolver closure.

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

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.3% 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

Unknown

Total

1

Last Release

3248d ago

### Community

Maintainers

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

---

Top Contributors

[![mindplay-dk](https://avatars.githubusercontent.com/u/103348?v=4)](https://github.com/mindplay-dk "mindplay-dk (28 commits)")[![toyosawa](https://avatars.githubusercontent.com/u/453999?v=4)](https://github.com/toyosawa "toyosawa (2 commits)")

### Embed Badge

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

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

###  Alternatives

[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

538204.9M23](/packages/league-uri-interfaces)[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[laudis/neo4j-php-client

Neo4j-PHP-Client is the most advanced PHP Client for Neo4j

184616.9k31](/packages/laudis-neo4j-php-client)[http-interop/response-sender

A function to convert PSR-7 Response to HTTP output

46711.5k40](/packages/http-interop-response-sender)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mezzio/mezzio-authentication-oauth2

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

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

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