phpdot/http-middleware

PSR-15 middlewares for PHPdot

Maintainers

Package info

github.com/phpdot/http-middleware

pkg:composer/phpdot/http-middleware

Transparency log

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-30 22:23 UTC

This package is auto-updated.

Last update: 2026-06-30 22:24:23 UTC


README

PSR-15 middlewares for PHPdot

Install

composer require phpdot/http-middleware

Quick Start

Every middleware implements PSR-15 MiddlewareInterface and depends only on PSR interfaces, so it runs in any PSR-15 stack:

use PHPdot\HttpMiddleware\JsonBodyMiddleware;

// $responses and $streams are any PSR-17 factories (e.g. nyholm/psr7's Psr17Factory)
$middleware = new JsonBodyMiddleware($responses, $streams);

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

Inside the phpdot framework

The middlewares are framework-agnostic — no phpdot/http dependency — so they slot into any PSR-15 pipeline. In a phpdot app, register one on the router:

$router->middleware(JsonBodyMiddleware::class);

The constructor's ResponseFactoryInterface and StreamFactoryInterface are resolved by the container (phpdot/http's ResponseFactory implements both).

Middlewares

JsonBodyMiddleware

Parses an application/json request body into the PSR-7 parsed body, so handlers read $request->getParsedBody() instead of hand-decoding. A malformed body is rejected with 400 — once, centrally — rather than surfacing as a misleading validation error downstream.

  • application/json is recognised (a charset suffix is fine); other content types pass through untouched.
  • An empty body or a top-level scalar is left unparsed, for the handler to treat as empty.
  • Malformed JSON → 400 {"error":"Malformed JSON request body."}, and the handler is not called.

PSR Standards

PSR Interface Usage
PSR-7 ServerRequestInterface Request the middleware reads and augments
PSR-7 ResponseInterface Return type
PSR-15 MiddlewareInterface The contract every middleware implements
PSR-15 RequestHandlerInterface The next handler in the pipeline
PSR-17 ResponseFactoryInterface Builds error responses (e.g. 400)
PSR-17 StreamFactoryInterface Builds response bodies

Development

composer test        # Run tests
composer analyse     # PHPStan level 10
composer cs-fix      # Fix code style
composer cs-check    # Check code style (dry run)
composer check       # Run all three

License

MIT