phpdot / http-middleware
PSR-15 middlewares for PHPdot.
Requires
- php: >=8.5
- psr/http-factory: ^1.0
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.94
- phpdot/http: ^0.2
- phpstan/phpstan: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
README
PSR-15 middlewares for PHPdot. Each is a small, framework-agnostic middleware that plugs into any PSR-15 pipeline.
| Class | Does |
|---|---|
Pipeline |
The PSR-15 dispatcher: runs a middleware list (outermost first) around a final handler. Itself a RequestHandlerInterface, so pipelines nest and any transport serves one directly. Stateless per call — one shared instance is coroutine-safe under Swoole. |
JsonBodyMiddleware |
Decodes application/json request bodies into the request's parsed body; rejects malformed JSON with 400. |
AjaxMiddleware |
Restricts a route to script-issued requests; a browser navigating to it gets a 404. A convenience, not a security control — the signals are client-supplied. |
Table of Contents
Requirements
| Requirement | Constraint |
|---|---|
| PHP | >= 8.5 |
psr/http-factory |
^1.0 |
psr/http-message |
^2.0 |
psr/http-server-handler |
^1.0 |
psr/http-server-middleware |
^1.0 |
Bring any PSR-7/PSR-17 implementation (for example phpdot/http).
Installation
composer require phpdot/http-middleware
Usage
Pipeline
Compose middlewares around a final handler; the pipeline is itself a PSR-15 handler:
use PHPdot\HttpMiddleware\JsonBodyMiddleware; use PHPdot\HttpMiddleware\Pipeline; $pipeline = new Pipeline( [new JsonBodyMiddleware($responseFactory, $streamFactory)], $appHandler, ); $response = $pipeline->handle($request);
Middlewares run in list order — the first is outermost. Each may short-circuit by returning a
response without delegating; when every middleware delegates, the final handler runs. Pipelines
nest: a Pipeline is a valid final handler for another Pipeline.
JsonBodyMiddleware
For requests with a application/json content type, it decodes the body and exposes it via
getParsedBody(); a malformed body returns a 400 with a JSON error, and non-JSON or empty bodies pass
straight through. Construct it with your PSR-17 response and stream factories and add it to the pipeline:
use PHPdot\HttpMiddleware\JsonBodyMiddleware; use PHPdot\HttpMiddleware\Pipeline; $pipeline = new Pipeline( [new JsonBodyMiddleware($responseFactory, $streamFactory)], $appHandler, );
// Downstream, the decoded body is available as the parsed body: $data = $request->getParsedBody();
Testing
composer install composer test # PHPUnit composer analyse # PHPStan, level max + strict rules composer cs-check # PHP-CS-Fixer composer check # All three
License
MIT.
This repository is a read-only mirror, generated by CI from phpdot/monorepo. Pull requests and issues belong in the monorepo.