2017 © Pedro Peláez
 

library middleware

Creates middleware layer on Zend Framework 2.

image

muriloamaral/middleware

Creates middleware layer on Zend Framework 2.

  • Wednesday, December 16, 2015
  • by muriloamaral
  • Repository
  • 3 Watchers
  • 4 Stars
  • 126 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 22 % Grown

The README.md

Build Status Coverage Status, (*1)

Middleware

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

Installation

With composer

Add this project in your composer.json:, (*3)

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

Now tell composer to download Middleware by running the command:, (*4)

$ php composer.phar update

Post installation

Enabling it in your config/application.config.php file., (*5)


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

Configuration

On your config file set your global and local middlewares. For instance:, (*6)

module/Application/config/module.config.php

```php, (*7)

// ... '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:, (*8)

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

), // ..., (*9)


Usage ----- Define your middleware classes: ```bash module/Application/src/Application/Middleware/First.php

```php, (*10)

namespace Application\Middleware;, (*11)

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

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

    $next(); // call the next middleware

    // Run code after all middlewares run
}

}, (*13)

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

```php, (*14)

namespace Application\Middleware;, (*15)

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

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

    $next(); // call the next middleware

    // Run code after all middlewares run
}

}, (*17)


#### Global scope Middlewares on global scope will be executed everytime a request is made. #### 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 -------------- #### Inject Service Locator It's also possible to access ServiceManager within your middleware classes. It's only necessary to implement ServiceLocatorAwareInterface. For instance: ```bash module/Application/src/Application/Middleware/First.php

```php, (*18)

namespace Application\Middleware;, (*19)

use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterface;, (*20)

class First implements ServiceLocatorAwareInterface { protected $serviceLocator;, (*21)

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;
}

}, (*22)


#### 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`. ```bash module/Application/src/Application/Middleware/First.php ``` ```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 ```bash module/Application/config/module.config.php ``` ```php // ... 'middlewares' => array( 'global' => array( 'Application\Middleware\First' ) ), // ... ``` 3. Configure the abstract service factory ```bash module/Application/config/module.config.php ``` ```php // ... 'service_manager' => array( // ... 'abstract_factories' => array( // ... 'Middleware\Factory\MiddlewareAbstractServiceFactory' ), // ... ), // ... ``` #### Configuration You can provide any callable as a middleware name. Such as functions, static methods and so on. For instance: ```php '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' ), ), ),

The Versions

16/12 2015

dev-master

9999999-dev https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

The Development Requires

middleware zf2 module zendframework zendframework2

16/12 2015

v1.0.0

1.0.0.0 https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

The Development Requires

middleware zf2 module zendframework zendframework2

07/04 2015

dev-custom_service_manager

dev-custom_service_manager https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

middleware zf2 module zendframework zendframework2

04/04 2015

dev-without_abstract_factory

dev-without_abstract_factory https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

middleware zf2 module zendframework zendframework2

03/04 2015

dev-callable_suport

dev-callable_suport https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

middleware zf2 module zendframework zendframework2

03/04 2015

dev-usefulnext

dev-usefulnext https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

middleware zf2 module zendframework zendframework2

03/04 2015

dev-local_config

dev-local_config https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

middleware zf2 module zendframework zendframework2

30/03 2015

dev-mvcEvent

dev-mvcEvent https://github.com/muriloacs/Middleware

Creates middleware layer on Zend Framework 2.

  Sources   Download

The Requires

 

middleware zf2 module zendframework zendframework2