PHPackages                             muriloamaral/middleware - 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. muriloamaral/middleware

ActiveLibrary[Framework](/categories/framework)

muriloamaral/middleware
=======================

Creates middleware layer on Zend Framework 2.

v1.0.0(10y ago)41612[1 issues](https://github.com/muriloacs/Middleware/issues)PHPPHP &gt;=5.3.3

Since Mar 30Pushed 10y ago3 watchersCompare

[ Source](https://github.com/muriloacs/Middleware)[ Packagist](https://packagist.org/packages/muriloamaral/middleware)[ Docs](https://github.com/muriloacs/Middleware)[ RSS](/packages/muriloamaral-middleware/feed)WikiDiscussions master Synced 1mo ago

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

[![Build Status](https://camo.githubusercontent.com/03d8ccc5ad06529d4b552f132700c99258b86c81bfe01bb3fb61b6897ea297df/68747470733a2f2f7472617669732d63692e6f72672f6d7572696c6f6163732f4d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/muriloacs/Middleware) [![Coverage Status](https://camo.githubusercontent.com/36b2b03dd72f5b4c77e610984690f172f357a3d932a2eca7015c983fbe75a3c4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d7572696c6f6163732f4d6964646c65776172652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/muriloacs/Middleware?branch=master)

Middleware
==========

[](#middleware)

Creates middleware layer on Zend Framework 2. Useful when it's necessary to make some work between route and controller dispatch phases.

Installation
------------

[](#installation)

#### With composer

[](#with-composer)

Add this project in your `composer.json`:

```
"require": {
    "muriloamaral/middleware": "dev-master"
}
```

Now tell composer to download Middleware by running the command:

```
$ php composer.phar update
```

#### Post installation

[](#post-installation)

Enabling it in your `config/application.config.php` file.

```
return array(
    'modules' => array(
        // ...
        'Middleware',
    ),
    // ...
);
```

Configuration
-------------

[](#configuration)

On your config file set your global and local middlewares. For instance:

```
module/Application/config/module.config.php
```

```
// ...
'middlewares' => array(
    'global' => array(
        'my.first.middleware',
        'my.second.middleware'
    ),
    'local' => array(
        'Application\Controller\IndexController' => array(
            'my.third.middleware'
        ),
    ),
),
// ...
'service_manager' => array(
    // ...
    'invokables' => array(
        // ...
        'my.first.middleware' => 'Application\Middleware\First',
        'my.second.middleware' => 'Application\Middleware\Second',
        // ...
    ),
    // ...
    'services' => array(
        // ...
        'my.third.middleware' => function($request, $response, $next) {
            // My code here. For instance:

            var_dump($request->getHeader('user-agent'));
            $next();
        },
        // ...
    ),
    // ...
),
// ...
```

Usage
-----

[](#usage)

Define your middleware classes:

```
module/Application/src/Application/Middleware/First.php
```

```
namespace Application\Middleware;

class First
{
    public function __invoke($request, $response, $next)
    {
        // My code here. For instance:

        var_dump($request->getHeader('user-agent'));

        $next(); // call the next middleware

        // Run code after all middlewares run
    }
}
```

```
module/Application/src/Application/Middleware/Second.php
```

```
namespace Application\Middleware;

class Second
{
    public function __invoke($request, $response, $next)
    {
        // My code here. For instance:

        var_dump($request->getHeader('user-agent'));

        $next(); // call the next middleware

        // Run code after all middlewares run
    }
}
```

#### Global scope

[](#global-scope)

Middlewares on global scope will be executed everytime a request is made.

#### Local scope

[](#local-scope)

Middlewares on local scope will be executed only if the current controller declares a middleware.

P.S: local middlewares are executed after global middlewares.

In this case, `my.first.middleware` and `my.second.middleware` will be always executed no matter what route is being called. Whereas `my.third.middleware` will be executed only when Application\\Controller\\IndexController is being called. Thus, if we access Application\\Controller\\IndexController first, second and third middlewares will be executed.

Advanced usage
--------------

[](#advanced-usage)

#### Inject Service Locator

[](#inject-service-locator)

It's also possible to access ServiceManager within your middleware classes. It's only necessary to implement ServiceLocatorAwareInterface. For instance:

```
module/Application/src/Application/Middleware/First.php
```

```
namespace Application\Middleware;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class First implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;

    public function __invoke($request, $next, $redirect)
    {
        // My code here. For instance:
        $config = $this->serviceLocator->get('config');
    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}
```

#### Abstract Service Factory

[](#abstract-service-factory)

If you don't want to declare middlewares inside your service manager config key, you can use the abstract service factory provided by us.

1. Define your middleware class, you need to implement `Middleware\MiddlewareInterface`.

    ```
    module/Application/src/Application/Middleware/First.php
    ```

    ```
    namespace Application\Middleware;

    use Closure;
    use Zend\Http\PhpEnvironment\Request;
    use Zend\Http\PhpEnvironment\Response;
    use Middleware\MiddlewareInterface;

    class First implements MiddlewareInterface
    {
        public function __invoke(Request $request, Response $response, Closure $next)
        {
            // My code here.
        }
    }
    ```
2. Configure your middleware

    ```
    module/Application/config/module.config.php
    ```

    ```
    // ...
    'middlewares' => array(
        'global' => array(
            'Application\Middleware\First'
        )
    ),
    // ...
    ```
3. Configure the abstract service factory

    ```
    module/Application/config/module.config.php
    ```

    ```
    // ...
    'service_manager' => array(
        // ...
        'abstract_factories' => array(
            // ...
            'Middleware\Factory\MiddlewareAbstractServiceFactory'
        ),
        // ...
    ),
    // ...
    ```

#### Configuration

[](#configuration-1)

You can provide any callable as a middleware name. Such as functions, static methods and so on. For instance:

```
'middlewares' => array(
    'global' => array(
        'my.first.middleware',
        'my.second.middleware',
        'MyNamespace\MyClass::MyStaticMethod', // Static method sample
        function ($request, $response, $next) // Function sample
        {
            var_dump($request->getHeader('user-agent'));
            $next();
        }
    ),
    'local' => array(
        'Application\Controller\IndexController' => array(
            'my.third.middleware'
        ),
    ),
),
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

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

3806d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6aad7356eb1c8df4fdecbf4b6e24607cf631eb367b3556d34e2c99fde0815f7a?d=identicon)[muriloamaral](/maintainers/muriloamaral)

---

Top Contributors

[![edipoReboucas](https://avatars.githubusercontent.com/u/974859?v=4)](https://github.com/edipoReboucas "edipoReboucas (70 commits)")[![muriloacs](https://avatars.githubusercontent.com/u/1087788?v=4)](https://github.com/muriloacs "muriloacs (10 commits)")[![vojtabiberle](https://avatars.githubusercontent.com/u/528942?v=4)](https://github.com/vojtabiberle "vojtabiberle (2 commits)")

---

Tags

middlewareZendFrameworkmodulezf2zendframework2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/muriloamaral-middleware/health.svg)

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

###  Alternatives

[melisplatform/melis-cms

Melis Platform CMS module

115.5k15](/packages/melisplatform-melis-cms)

PHPackages © 2026

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