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 takes 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()
0 commit comments