muriloamaral/middleware

此包最新版本(v1.0.0)没有可用的许可信息。

在Zend Framework 2上创建中间件层。

v1.0.0 2015-12-16 20:10 UTC

README

Build Status Coverage Status

中间件

在Zend Framework 2上创建中间件层。当需要在路由和控制器分发阶段之间执行某些工作时有用。

安装

使用composer

将此项目添加到您的 composer.json

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

现在运行以下命令以让composer下载Middleware:

$ php composer.phar update

安装后

在您的 config/application.config.php 文件中启用它。

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

配置

在您的配置文件中设置全局和本地中间件。例如

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();
        },
        // ...
    ),
    // ...
),
// ...

使用方法

定义您的中间件类

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

全局范围

全局范围的中间件将在每次请求时执行。

本地范围

只有当当前控制器声明了中间件时,本地范围的中间件才会执行。

备注:本地中间件在全局中间件之后执行。

在这种情况下,my.first.middlewaremy.second.middleware 将始终执行,无论调用的是哪个路由。而 my.third.middleware 只会在调用 Application\Controller\IndexController 时执行。因此,如果我们首先访问 Application\Controller\IndexController,则第一个、第二个和第三个中间件将执行。

高级使用

注入服务定位器

您也可以在中间件类中访问ServiceManager。只需实现ServiceLocatorAwareInterface即可。例如

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

抽象服务工厂

如果您不想在服务管理器的配置键中声明中间件,可以使用我们提供的抽象服务工厂。

  1. 定义您的中间件类,您需要实现 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. 配置您的中间件

    module/Application/config/module.config.php
    // ...
    'middlewares' => array(
        'global' => array(
            'Application\Middleware\First'
        )
    ),
    // ...
  3. 配置抽象服务工厂

    module/Application/config/module.config.php
    // ...
    'service_manager' => array(
        // ...
        'abstract_factories' => array(
            // ...
            'Middleware\Factory\MiddlewareAbstractServiceFactory'
        ),
        // ...
    ),
    // ...

配置

您可以将任何可调用项作为中间件名称提供。例如函数、静态方法等。例如

'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'        
        ),
    ),
),