You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+57-42Lines changed: 57 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -722,56 +722,71 @@ $middlewares = new MiddlewareRunner([
722
722
723
723
#### RequestBodyParserMiddleware
724
724
725
-
The `RequestBodyParserMiddleware` takes a fully buffered request body (generally from [`RequestBodyBufferMiddleware`](#requestbodybuffermiddleware)),
726
-
and parses the forms and uploaded files from the request body.
727
-
728
-
Parsed submitted forms will be available from `$request->getParsedBody()` as
729
-
array.
730
-
For example the following submitted body (`application/x-www-form-urlencoded`):
731
-
732
-
`bar[]=beer&bar[]=wine`
733
-
734
-
Results in the following parsed body:
725
+
The `RequestBodyParserMiddleware` takes a fully buffered request body
726
+
(generally from [`RequestBodyBufferMiddleware`](#requestbodybuffermiddleware)),
727
+
and parses the form values and file uploads from the incoming HTTP request body.
728
+
729
+
This middleware handler take care of applying values from HTTP
730
+
requests that use `Content-Type: application/x-www-form-urlencoded` or
731
+
`Content-Type: multipart/form-data` to resemble PHP's default superglobals
732
+
`$_POST` and `$_FILES`.
733
+
Instead of relying on these superglobals, you can use the
734
+
`$request->getParsedBody()` and `$request->getUploadedFiles()` methods
735
+
as defined by PSR-7.
736
+
737
+
Accordingly, each file upload will be represented as instance implementing [`UploadedFileInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#36-psrhttpmessageuploadedfileinterface).
738
+
Due to its blocking nature, the `moveTo()` method is not available and throws
739
+
a `RuntimeException` instead.
740
+
You can use `$contents = (string)$file->getStream();` to access the file
741
+
contents and persist this to your favorite data store.
735
742
736
743
```php
737
-
$parsedBody = [
738
-
'bar' => [
739
-
'beer',
740
-
'wine',
741
-
],
742
-
];
743
-
```
744
-
745
-
Aside from `application/x-www-form-urlencoded`, this middleware handler
746
-
also supports `multipart/form-data`, thus supporting uploaded files available
747
-
through `$request->getUploadedFiles()`.
748
-
749
-
The `$request->getUploadedFiles(): array` will return an array with all
750
-
uploaded files formatted like this:
751
-
752
-
```php
753
-
$uploadedFiles = [
754
-
'avatar' => new UploadedFile(/**...**/),
755
-
'screenshots' => [
756
-
new UploadedFile(/**...**/),
757
-
new UploadedFile(/**...**/),
758
-
],
759
-
];
760
-
```
744
+
$handler = function (ServerRequestInterface $request) {
745
+
// If any, parsed form fields are now available from $request->getParsedBody()
new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
767
772
new RequestBodyParserMiddleware(),
768
-
function (ServerRequestInterface $request, callable $next) {
769
-
// If any, parsed form fields are now available from $request->getParsedBody()
770
-
return new Response(200);
771
-
},
772
-
]);
773
+
$handler
774
+
]));
773
775
```
774
776
777
+
See also [example #12](examples) for more details.
778
+
779
+
> Note that this middleware handler simply parses everything that is already
780
+
buffered in the request body.
781
+
It is imperative that the request body is buffered by a prior middleware
782
+
handler as given in the example above.
783
+
This previous middleware handler is also resposible for rejecting incoming
784
+
requests that exceed allowed message sizes (such as big file uploads).
785
+
If you use this middleware without buffering first, it will try to parse an
786
+
empty (streaming) body and may thus assume an empty data structure.
787
+
See also [`RequestBodyBufferMiddleware`](#requestbodybuffermiddleware) for
788
+
more details.
789
+
775
790
#### Third-Party Middleware
776
791
777
792
A non-exhaustive list of third-party middleware can be found at the [`Middleware`](https://github.com/reactphp/http/wiki/Middleware) wiki page.
0 commit comments