PHPackages                             idealo/php-middleware-stack - 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. idealo/php-middleware-stack

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

idealo/php-middleware-stack
===========================

Implementation of HTTP Middleware PSR-15 specification

v3.0.1(1y ago)318.9k↓100%3Apache-2.0PHPPHP ^8.1CI passing

Since Jan 4Pushed 6mo ago5 watchersCompare

[ Source](https://github.com/idealo/php-middleware-stack)[ Packagist](https://packagist.org/packages/idealo/php-middleware-stack)[ RSS](/packages/idealo-php-middleware-stack/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (9)Used By (0)

PHP Middleware Stack
====================

[](#php-middleware-stack)

[![Build Status](https://github.com/idealo/php-middleware-stack/workflows/CI/badge.svg)](https://github.com/idealo/php-middleware-stack/actions?query=workflow%3Aci)[![Packagist](https://camo.githubusercontent.com/c7ac45a39166273e4c4d09857fe950fc98fa2e21ab167e53b13151a12f688ce3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696465616c6f2f7068702d6d6964646c65776172652d737461636b)](https://packagist.org/packages/idealo/php-middleware-stack)

This is an implementation of [PSR-15](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-15-request-handlers.md) using the proposed Interface packages [psr/http-server-middleware](https://github.com/php-fig/http-server-middleware)and [psr/http-server-handler](https://github.com/php-fig/http-server-handler) for PHP ^8.1 runtime environment.

It enables a sequential execution of middlewares that use a PSR-7 conform Response/Request implementation.

Install
-------

[](#install)

```
composer require idealo/php-middleware-stack
```

Note: use Version ^2.0 for PHP &lt; 8.1

How to
------

[](#how-to)

```
use Idealo\Middleware\Stack;

$stack = new Stack(
    $defaultResponse,
    $middleware1,
    $middleware2,
    $middleware3
);

$stackResponse = $stack->handle($request);
```

Usage
-----

[](#usage)

**idealo/php-middleware-stack** provides the `Idealo\Middleware\Stack` class. All it has to know in order to be instantiable is:

- an instance of `Psr\Http\Message\ResponseInterface` as the default response
- and middlewares, that implement the `Psr\Http\Server\MiddlewareInterface`

To perform a sequential processing of injected middlewares you have to call stack's `handle` method with:

- an instance of `Psr\Http\Message\ServerRequestInterface`.

By default stack's `handle` method returns the injected response object. If any middleware decides to answer on it's own, than the response object of this certain middleware is returned.

Stack implements `Psr\Http\Server\RequestHandlerInterface`.

For example
-----------

[](#for-example)

```
// you decide what middleware you want to put in a stack.
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

class TrickyMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
    {
        $requestBody = $request->getBody();
        try {
            // implement your middleware logic here
        } catch (\Exception $exception){
            return new CustomExceptionResponse($exception);
        }

        return $handler->handle($request);
    }
}

class VeryTrickyMiddleware implements MiddlewareInterface
{
    ...
}

class LessTrickyMiddleware implements MiddlewareInterface
{
    ...
}

// you define your PSR7 conform response instance
$defaultResponse = new DefaultResponse();

// you put your request into a PSR7 conform way
$request = new ServerRequest();

// and here we are
$stack = new \Idealo\Middleware\Stack(
    $defaultResponse,
    new TrickyMiddleware(),
    new VeryTrickyMiddleware(),
    new LessTrickyMiddleware()
);

$stackResponse = $stack->handle($request);

// if everything goes well then
var_dump($stackResponse === $defaultResponse); // gives: true
```

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance59

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 84.2% 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 ~491 days

Recently: every ~610 days

Total

7

Last Release

466d ago

Major Versions

v0.2.0 → v1.0.02018-05-25

v1.0.0 → v2.0.02021-03-14

v2.0.1 → v3.0.02024-10-21

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

v2.0.0PHP &gt;=7.3

v3.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e019a2502a0587807795f25698b4a8b331f9f1910a2a3f2237dc3e7ce8af48a?d=identicon)[zavalit](/maintainers/zavalit)

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

---

Top Contributors

[![dirx](https://avatars.githubusercontent.com/u/795259?v=4)](https://github.com/dirx "dirx (48 commits)")[![zavalit](https://avatars.githubusercontent.com/u/650816?v=4)](https://github.com/zavalit "zavalit (8 commits)")[![datitran](https://avatars.githubusercontent.com/u/3866530?v=4)](https://github.com/datitran "datitran (1 commits)")

---

Tags

php7psr-15httpresponserequestpsrpsr-7middlewarestackpsr-15

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/idealo-php-middleware-stack/health.svg)

```
[![Health](https://phpackages.com/badges/idealo-php-middleware-stack/health.svg)](https://phpackages.com/packages/idealo-php-middleware-stack)
```

###  Alternatives

[psr/http-server-middleware

Common interface for HTTP server-side middleware

18091.2M1.5k](/packages/psr-http-server-middleware)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k692.9M1.9k](/packages/psr-http-factory)[psr/http-server-handler

Common interface for HTTP server-side request handler

175101.3M918](/packages/psr-http-server-handler)[mezzio/mezzio

PSR-15 Middleware Microframework

3883.6M96](/packages/mezzio-mezzio)[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[mezzio/mezzio-authentication

Authentication middleware for Mezzio and PSR-7 applications

121.6M26](/packages/mezzio-mezzio-authentication)

PHPackages © 2026

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