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

ActiveLibrary

vakata/middleman
================

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

4.0.0(8y ago)0881LGPL-3.0+PHPPHP &gt;=7.0

Since Nov 6Pushed 8y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (9)Versions (11)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 between major releases, please see [UPGRADING.md](UPGRADING.md).

[![PHP Version](https://camo.githubusercontent.com/98e06ab279ef7d8f5dc610b12861b445a427f985f0ced740b6982d8fac4b3fe8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e302532422d626c75652e737667)](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)

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

You can implement simple middleware "in place" by using anonymous functions in a middleware-stack:

```
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

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

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

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

To implement reusable middleware components, you should implement the PSR-15 [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php).

```
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\ServerMiddleware\MiddlewareInterface;

class MyMiddleware implements MiddlewareInteface
{
    public function process(ServerRequestInterface $request, DelegateInterface $delegate) {
        // ...
    }
}
```

If you want to integrate with a [DI 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

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 86% 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 ~127 days

Total

8

Last Release

2951d ago

Major Versions

1.1.0 → 2.0.02016-09-29

2.0.1 → 3.0.02017-11-29

3.0.2 → 4.0.02018-04-16

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

3.0.0PHP &gt;=7.0

### Community

Maintainers

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

---

Top Contributors

[![mindplay-dk](https://avatars.githubusercontent.com/u/103348?v=4)](https://github.com/mindplay-dk "mindplay-dk (37 commits)")[![fetchandadd](https://avatars.githubusercontent.com/u/15845152?v=4)](https://github.com/fetchandadd "fetchandadd (2 commits)")[![vakata](https://avatars.githubusercontent.com/u/146052?v=4)](https://github.com/vakata "vakata (2 commits)")[![Brammm](https://avatars.githubusercontent.com/u/851445?v=4)](https://github.com/Brammm "Brammm (1 commits)")[![simoheinonen](https://avatars.githubusercontent.com/u/3840367?v=4)](https://github.com/simoheinonen "simoheinonen (1 commits)")

### Embed Badge

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

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

###  Alternatives

[cakephp/cakephp

The CakePHP framework

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

Bref is a framework to write and deploy serverless PHP applications on AWS Lambda.

3.4k9.6M55](/packages/bref-bref)[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)[windwalker/framework

The next generation PHP framework.

25639.1k1](/packages/windwalker-framework)

PHPackages © 2026

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