Support for PUT, PATCH, DELETE verbs exist (Eg \PhpExpress\Application::put() and \PhpExpress\Application::patch()) but are of limited use because there is no payload data populated into in the Request object.
Populate the $req->body with the payload from PHP's Standard Input stream via PHP's parse_str() when $_POST is empty. For simplicity, since the $req->body is just a pointer to $_POST superglobal, we can parse the data into $_POST directly which is guaranteed to be empty for any HTTP request method other than POST.
Forms can be submitted with different encoding types in the Content-Type header. application/x-www-form-urlencoded is the default, but multipart/form-data could also be accepted. However handling files uploaded with multipart/form-data encoding via HTTP PUT is not supported in PHP <8.4, therefore the parsing has to be done in different ways.
Full and future-proof solution, for when php 8.4 drops in November 2024:
// Make data payloads available in $this->body for other HTTP verbs.
if (
$this->method !== 'POST'
&& $this->is('application/x-www-form-urlencoded')
) {
parse_str(file_get_contents('php://input'), $_POST);
}
// Add support for multipart/form-data parsing for other HTTP verbs in PHP >=8.4.
if (
$this->method !== 'POST'
&& $this->is('multipart/form-data')
&& function_exists('request_parse_body')
) {
[$_POST, $_FILES] = call_user_func('request_parse_body');
}
Problem statement
Support for PUT, PATCH, DELETE verbs exist (Eg
\PhpExpress\Application::put()
and\PhpExpress\Application::patch()
) but are of limited use because there is no payload data populated into in theRequest
object.Steps to reproduce
But the actual result varies by HTTP Request Method.
Only the POST request type matches the expected result above with a populated $req->body
All others show an empty body:
Proposed Resolution
Populate the
$req->body
with the payload from PHP's Standard Input stream via PHP'sparse_str()
when $_POST is empty. For simplicity, since the $req->body is just a pointer to $_POST superglobal, we can parse the data into$_POST
directly which is guaranteed to be empty for any HTTP request method other than POST.Forms can be submitted with different encoding types in the
Content-Type
header.application/x-www-form-urlencoded
is the default, butmultipart/form-data
could also be accepted. However handling files uploaded withmultipart/form-data
encoding via HTTP PUT is not supported in PHP <8.4, therefore the parsing has to be done in different ways.Full and future-proof solution, for when php 8.4 drops in November 2024: