Http-Middleware

Http-Middleware supporting PSR-15.

Introduction

Http-Middleware is a standalone component supporting PSR-15.

Accpeted as awesome PSR-15 middleware package

Installation

It’s recommended that you use Composer to install Moon Http-Middleware.

$ php composer.phar require moon-php/http-middleware

This will install Http-Middleware, it requires PHP 7.1 or newer.

Usage

Delegate

According to the PSR-15 proposal :

The DelegateInterface defines a single method that accepts a request and returns a response.

The delegate interface must be implemented by any middleware dispatcher that uses middleware implementing MiddlewareInterface.

In Moon Http-Middleware the Delegate is a Middleware Dispatcher itself.

The Delegate is really simple object, it has a constructor that requires 3 arguments.

  • An array of middleware/string.
  • A callable to use as default response
  • Optionally a PSR-11 Container for lazy loading middleware.

The middlewares passed in the array can be immediately PSR-15 MiddlewareInterface instances or strings that will be resolved when needed by a PSR-11 Container.

When resolved by the container a PSR-15 MiddlewareInterface instance is expected to be returned.

<?php
$request = new ServerRequestImplementor();
$delegate = new Delegate([new MiddlewareOne(), new MiddlewareTwo(), new MiddlewareThree()], new DefaultResponse());
$delegate->process($request);

As said before, it’s also possible to pass a PSR-7 Container to the Delegate that will instantiate/load the objects at runtime.

This approach is preferable because will not load all the Middlewares before the execution.

<?php
$container = new Container(); // This container will retrieve the middleware at runtime. $container->get('middlewareOne');
$request = new ServerRequestImplementor();
$delegate = new Delegate(['middlewareOne', 'middlewareTwo', 'middlewareThree'], new DefaultResponse(), $container);
$delegate->process($request);

InvalidArgumentException

While the Delegate is processing the middleware stack if an invalid middleware is used, an InvalidArgumentException will be thrown.