PHPackages                             activecollab/middlewarestack - 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. [Framework](/categories/framework)
4. /
5. activecollab/middlewarestack

ActiveLibrary[Framework](/categories/framework)

activecollab/middlewarestack
============================

Port of Slim's middleware stack implementation as a stand-alone package

2.0.0(4mo ago)052.3k↓34%MITPHPPHP &gt;=8.0

Since Nov 3Pushed 4mo ago2 watchersCompare

[ Source](https://github.com/activecollab/middlewarestack)[ Packagist](https://packagist.org/packages/activecollab/middlewarestack)[ Docs](https://labs.activecollab.com)[ RSS](/packages/activecollab-middlewarestack/feed)WikiDiscussions master Synced 1mo ago

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

Middleware Stack
================

[](#middleware-stack)

[![Build Status](https://camo.githubusercontent.com/d25b2108c613ae5421eb777520b16a0e1c514be2d40ad27aded8524bd3ca0d1b/68747470733a2f2f7472617669732d63692e6f72672f616374697665636f6c6c61622f6d6964646c6577617265737461636b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/activecollab/middlewarestack)

This package lets you build a stack of middlewares and run requests through them to their coresponding responses. Stack is Last In First Out stack (LIFO), meaning that middlewares that are added later are considered to be "outter" middlewares, and they are executed first.

Example:

```
$stack = new MiddlewareStack();

$stack->addMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {
   // Route request to the contraoller, execute action, and encode action result to response

   if ($next) {
       $response = $next($request, $response);
   }

   return $response;
});

$stack->addMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {
   if (!user_is_authenticated($request)) {
       return $response->withStatus(403); // Break here if user is not authenticated
   }

   if ($next) {
       $response = $next($request, $response);
   }

   return $response;
});

$request = new ServerRequest();
$response = $stack->process($request, new Response());
```

This example shows a simple authorization check prior to request being sent further down to routing, controller, and result encoding.

Error Handling
--------------

[](#error-handling)

Stack exposes a way to handle exceptions (which extend `\Exception` class) and PHP errors (which are `\Throwable`, but don't extend `\Exception` class, available in PHP7 and up):

```
$stack->setExceptionHandler(function (Exception $e, ServerRequestInterface $request, ResponseInterface $response) {
    $response = $response->withStatus(500, 'Exception: ' . $e->getMessage());

    return $response;
});

$stack->setPhpErrorHandler(function (Throwable $e, ServerRequestInterface $request, ResponseInterface $response) {
    $response = $response->withStatus(500, 'PHP error: ' . $e->getMessage());

    return $response;
});
```

Extension Points
----------------

[](#extension-points)

You can use `MiddlewareStack` implementation as is, or you can extend it to change its behaviour. There are two extra `protected` methods that you can use to hook in your behaviour in the stack execution:

1. `MiddlewareStack` is called in the middle of execution, as just another middleware. Override `__invoke` method if you need to inject extra functionlity there (like routing, with per-route middleware stack for example),
2. `finalizeProcessing` is called prior to $response being returned by `process` method. Override if you need to do something with response prior to returning it.

History
-------

[](#history)

Most middleware implementations that we found in November 2016. assumed and did too much, being mini frameworks themselves - they anticipated routing, substacking etc.

[Slim framework](http://www.slimframework.com) has a nice middleware stack implementation, but it was not available as a stand-alone component, something that we needed, so we decided to extract it.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance75

Regular maintenance activity

Popularity28

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~669 days

Recently: every ~836 days

Total

6

Last Release

135d ago

Major Versions

0.10.3 → 1.0.02016-11-10

1.0.0 → 2.0.02026-01-03

PHP version history (2 changes)0.10.0PHP &gt;=5.6.0

2.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/729914?v=4)[Ilija Studen](/maintainers/ilijastuden)[@ilijastuden](https://github.com/ilijastuden)

---

Top Contributors

[![ilijastuden](https://avatars.githubusercontent.com/u/729914?v=4)](https://github.com/ilijastuden "ilijastuden (13 commits)")

---

Tags

middlewarepsr7

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/activecollab-middlewarestack/health.svg)

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

###  Alternatives

[slim/psr7

Strict PSR-7 implementation

14818.0M411](/packages/slim-psr7)[cakephp/authentication

Authentication plugin for CakePHP

1153.6M67](/packages/cakephp-authentication)[slim/csrf

Slim Framework 4 CSRF protection PSR-15 middleware

3512.1M94](/packages/slim-csrf)[slim/http

Slim PSR-7 Object Decorators

1502.3M66](/packages/slim-http)[slim/http-cache

Slim Framework HTTP cache middleware and service provider

1242.9M27](/packages/slim-http-cache)[davidepastore/slim-validation

A slim middleware for validation based on Respect/Validation

171223.7k3](/packages/davidepastore-slim-validation)

PHPackages © 2026

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