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(6mo ago)055.2k↓38%MITPHPPHP &gt;=8.0

Since Nov 3Pushed 6mo 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 yesterday

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

49

—

FairBetter than 94% of packages

Maintenance69

Regular maintenance activity

Popularity29

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

181d 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

[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k656.1k38](/packages/neuron-core-neuron-ai)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[cakephp/authentication

Authentication plugin for CakePHP

1214.1M103](/packages/cakephp-authentication)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)

PHPackages © 2026

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