gadwinn / http-method-middleware
PSR-15 middlewares: HTTP method override for HTML forms and body parsing (urlencoded, JSON) for the requests PHP does not parse.
Requires
- php: >=8.3
- psr/http-message: ^1.1 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpdocumentor/shim: *
- phpstan/phpstan: *
- phpunit/phpunit: *
- povils/phpmnd: *
- roave/security-advisories: dev-latest@dev
- squizlabs/php_codesniffer: *
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-18 11:17:29 UTC
README
Two independent PSR-15 middlewares around HTTP methods:
MethodOverrideMiddlewarelets an HTML form (limited to GET and POST) send aPUT,PATCHorDELETErequest.BodyParserMiddlewarefills the parsed body of the requests PHP does not parse by itself (PUT/PATCH/DELETEforms, JSON).
Use one, the other, or both. No dependency besides the PSR interfaces, no global state, nothing read from php://input or the superglobals.
Requirements
- PHP >= 8.3
Installation
composer require gadwinn/http-method-middleware
MethodOverrideMiddleware
use G\HttpMethodMiddleware\MethodOverrideMiddleware; $pipeline->pipe(new MethodOverrideMiddleware());
<form method="post" action="/articles/42"> <input type="hidden" name="_method" value="DELETE"> <button>Delete</button> </form>
The request reaches the next middleware (your router) as a DELETE.
The method is read from the _method field of the parsed body, then from the X-HTTP-Method-Override header. For safety:
- only
POSTrequests can be overridden (never aGET, which a simple link could trigger); - only
PUT,PATCHandDELETEare accepted; any other value is ignored and the request stays aPOST.
Everything is configurable:
new MethodOverrideMiddleware( allowedMethods: ['PUT', 'PATCH', 'DELETE'], field: '_method', // '' to ignore the body header: 'X-HTTP-Method-Override', // '' to ignore the header );
Place it before the router, and before a CSRF middleware that only checks some methods.
BodyParserMiddleware
use G\HttpMethodMiddleware\BodyParserMiddleware; $pipeline->pipe(new BodyParserMiddleware()); // In a handler: $data = $request->getParsedBody(); // ['title' => 'Hello']
PHP only fills $_POST for POST requests sent as a form. This middleware parses the body, read from the PSR-7 stream, for:
| Content-Type | Methods | Result |
|---|---|---|
application/x-www-form-urlencoded |
any (in practice PUT, PATCH, DELETE) |
same array as $_POST would give |
application/json, application/*+json |
any | decoded array (objects become associative arrays) |
The request is passed on unchanged when it already has a non-empty parsed body, or when its body is empty, of another content type, or not valid (malformed JSON, JSON scalar): check getParsedBody() in your handler and answer 400 if the data you expect is missing.
multipart/form-data (file uploads) is not parsed: send uploads with POST, which PHP handles natively.
Using both
$pipeline->pipe(new MethodOverrideMiddleware()); $pipeline->pipe(new BodyParserMiddleware()); $pipeline->pipe($router);
Development
composer install composer test-all
License
MIT