starchphp/middleware

A PSR-15 middleware and requestHandler handler


License
MIT

Documentation

Starch Middleware

Build Status Scrutinizer Code Quality Coverage Status

Super simple PSR-15 request and middleware handling.

Middle-what now?

A middleware component is an individual component participating, often together with other middleware components, in the processing of an incoming request and the creation of a resulting response, as defined by PSR-7.

A middleware component MAY create and return a response without delegating to a request handler, if sufficient conditions are met.

Installation

Install using composer

composer install starchphp/middleware

Usage

Construct a new Stack with:

  • An array of MiddlewareInterfaces. These must be instances of the interface. No strings, closures... If you wish to use the latter, I propose you resolve these dependencies before constructing the stack
  • A RequestHandlerInterface instance that will be treated as the inner most layer of the stack. This can be a request handler that's tied to a route in your application for example.

Once the stack has been initiated, dispatch a request to it, it will return a ResponseInterface instance.

use Starch\Middleware\Stack;

// $request = Request::fromGlobals();
// $requestHandler = $router->dispatch($request);

$stack = new Stack([
    new ExceptionMiddleware(),
    new CorsMiddleware(),
    new AuthorizationMiddleware(),
    new ValidationMiddleware(),
], $requestHandler);

$response = $stack->dispatch($request);