jerowork/middleware-request-handler

此包已被废弃,不再维护。没有建议的替代包。

基于PSR-15的简约中间件请求处理器(分发器)

0.5.0 2019-04-05 19:23 UTC

This package is auto-updated.

Last update: 2019-11-21 18:01:17 UTC


README

基于PSR-15的简约中间件请求处理器(分发器)。

安装

通过Composer安装

$ composer require jerowork/middleware-request-handler

用法

与PSR-7请求实现一起使用,例如 Zend Diactoros

示例

use Jerowork\MiddlewareDispatcher\Middleware\FinalResponseMiddleware;
use Jerowork\MiddlewareDispatcher\MiddlewareRequestHandler;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\SapiEmitter;
use Zend\Diactoros\ServerRequestFactory;

// Setup a list of PSR-15 middlewares
$middlewares = [
    new SomeMiddleware(),
    new AnotherMiddleware()
];

// Setup request handler
$handler = new MiddlewareRequestHandler($middlewares);

// Add other middlewares after construct
$handler->addMiddleware(
    new ThirdMiddleware(),
    new FourthMiddleware()
);

// Add final reversal order middleware
$handler->addMiddleware(new FinalResponseMiddleware(new Response());

// Handle a PSR-7 server request to response by the request handler (PSR-15)
$response = $handler->handle(ServerRequestFactory::fromGlobals());

// Output PSR-7 response with a response emitter implementation of your choice
(new SapiEmitter())->emit($response);