stratify / http
HTTP middlewares for PSR-7 and PSR-15
Requires
- php: >=7.4
- laminas/laminas-diactoros: ^2.0
- laminas/laminas-httphandlerrunner: ^1.1
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-11-29 03:58:21 UTC
README
HTTP middleware utilities built upon:
composer require stratify/http
Middlewares
A middleware can be either an instance of Psr\Http\Server\MiddlewareInterface
:
class MyMiddleware implements \Psr\Http\Server\MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { return new Response(...); } } $middleware = new MyMiddleware;
or a simple callable, which allows to use closures for quickly writing middlewares:
$middleware = function(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { return new Response(...); }
Middleware pipe
The middleware pipe let us pipe middlewares to execute one after the other. It is similar to using the pipe (|) operator on the command line.
It's interesting to note that the pipe is also a middleware, which means it can be nested or combined with any other middleware.
Usage:
$middleware = new Pipe([ new Middleware1, new Middleware2, // ... ]); // Run $response = $middleware->process($request, $handler);
The pipe will first execute Middleware1
. If that middleware calls $next
then Middleware2
will be executed. An infinite number of middlewares can be piped together.
If you don't need to use the $handler
argument for the pipe, you can use the LastHandler
class:
$response = $middleware->process($request, new \Stratify\Http\Middleware\LastHandler);