stratify/http

HTTP middlewares for PSR-7 and PSR-15

0.6.0 2020-08-02 09:03 UTC

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