slince / middleware
灵活的PSR15中间件分发器
2.0.1
2019-04-30 11:38 UTC
Requires
- php: >=7.0
- http-interop/http-server-middleware: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.0|^6.0
- zendframework/zend-diactoros: ^1.0
This package is auto-updated.
Last update: 2024-08-29 05:06:05 UTC
README
该包是一个灵活的PSR-15中间件分发器,用于PSR-7请求消息,有助于创建中间件和分发。
安装
通过composer安装
composer require slince/middleware
快速示例
$dispatcher = new Slince\Middleware\Dispatcher([$middleware1, $middleware2]); $response = $dispatcher->process(Zend\Diactoros\ServerRequestFactory::fromGlobals()); var_dump($response instanceof Psr\Http\Message\ResponseInterface);
用法
添加中间件
将PSR-15中间件添加到队列中
use Psr\Http\Message\ServerRequestInterface; use Interop\Http\Server\MiddlewareInterface; use Interop\Http\Server\RequestHandlerInterface; use Zend\Diactoros\Response; class MyMiddleware implements MiddlewareInteface { public function process(ServerRequestInterface $request, RequestHandlerInterface $next) { $response = new Response(); $response->getBody()->write('hello world'); return $response; } } $dispatcher = new Slince\Middleware\Dispatcher([ new MyMiddleware() ]);
或者直接添加可调用的函数
$dispatcher->push(function(ServerRequestInterface $request, RequestHandlerInterface $next){ return $delegate->process($request); });
分发
try { $response = $dispatcher->process(Zend\Diactoros\ServerRequestFactory::fromGlobals()); } catch (Slince\Middleware\Exception\MissingResponseException $exception) { //... }
如果中间件没有返回无效响应或队列耗尽,将抛出 MissingResponseException
许可证
MIT许可证。见 MIT