phpdot / http-middleware
PSR-15 middlewares for PHPdot
Requires
- php: >=8.4
- 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
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.0
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/jsonis recognised (acharsetsuffix 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