phpdot/http-middleware
Composer 安装命令:
composer require phpdot/http-middleware
包简介
PSR-15 middlewares for PHPdot
README 文档
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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-30