kovagoz/http-middleware-closure-handler

PSR-15 compatible HTTP middleware which can utilize closure type request handlers.

1.1.2 2022-07-30 16:24 UTC

This package is auto-updated.

Last update: 2024-04-29 05:05:33 UTC


README

PSR-15 compatible HTTP middleware which can utilize closure type request handlers.

If you don't want to bother about those ugly HTTP controllers and love simplicity, this middleware is your best friend.

phpunit workflow

Requirements

  • PHP >=8.0

Usage

$request = $serverRequestFactory
    ->createServerRequest('GET', '/')
    ->withAttribute('__handler', fn() => 'hello world!');

$middleware = new ClosureHandler($responseFactory, $streamFactory);

$response = $middleware->process($request, $nextHandler);

echo $response->getBody(); // Will print "hello world!"

If no handler found in the request object, or handler is not closure, then the middleware passes the request to the next middleware in the row.

Name of the request attribute (__handler) can be changed by the watchRequestAttribute() method.