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: core/file-upload.md
+42-17Lines changed: 42 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,23 @@ before proceeding. It will help you get a grasp on how the bundle works, and why
11
11
**Note**: Uploading files won't work in `PUT` or `PATCH` requests, you must use `POST` method to upload files.
12
12
See [the related issue on Symfony](https://github.com/symfony/symfony/issues/9226) and [the related bug in PHP](https://bugs.php.net/bug.php?id=55815) talking about this behavior.
13
13
14
+
Previously to API Platform 3.3, file upload was using controllers that needs:
15
+
16
+
```yaml
17
+
api_platform:
18
+
use_symfony_listeners: true
19
+
```
20
+
21
+
Since 3.3, we recommend to use a Processor, note that you need to enable the multipart format globally:
22
+
23
+
```yaml
24
+
api_platform:
25
+
use_symfony_listeners: false
26
+
formats:
27
+
multipart: ['multipart/form-data']
28
+
jsonld: ['application/ld+json']
29
+
```
30
+
14
31
## Installing VichUploaderBundle
15
32
16
33
Install the bundle with the help of Composer:
@@ -63,7 +80,7 @@ use ApiPlatform\Metadata\Get;
63
80
use ApiPlatform\Metadata\GetCollection;
64
81
use ApiPlatform\Metadata\Post;
65
82
use ApiPlatform\OpenApi\Model;
66
-
use App\Controller\CreateMediaObjectAction;
83
+
use App\State\SaveMediaObject;
67
84
use Doctrine\ORM\Mapping as ORM;
68
85
use Symfony\Component\HttpFoundation\File\File;
69
86
use Symfony\Component\Serializer\Annotation\Groups;
@@ -75,12 +92,13 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich;
Note: From V3.3 onwards, `'multipart/form-data'` must either be including in the global API-Platform config, either in `formats` or `defaults->inputFormats`, or defined as an `inputFormats` parameter on an operation by operation basis.
129
147
130
-
### Creating the Controller
148
+
### Creating the Processor
131
149
132
-
At this point, the entity is configured, but we still need to write the action
150
+
At this point, the entity is configured, but we still need to write the processor
133
151
that handles the file upload.
134
152
135
153
```php
136
154
<?php
137
-
// api/src/Controller/CreateMediaObjectAction.php
155
+
// api/src/State/SaveMediaObject.php
138
156
139
-
namespace App\Controller;
157
+
namespace App\State;
140
158
159
+
use ApiPlatform\Metadata\Operation;
160
+
use ApiPlatform\State\ProcessorInterface;
141
161
use App\Entity\MediaObject;
142
-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
143
-
use Symfony\Component\HttpFoundation\Request;
144
-
use Symfony\Component\HttpKernel\Attribute\AsController;
162
+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
145
163
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
164
+
use Vich\UploaderBundle\Storage\StorageInterface;
146
165
147
-
#[AsController]
148
-
final class CreateMediaObjectAction extends AbstractController
166
+
final class SaveMediaObject implements ProcessorInterface
149
167
{
150
-
public function __invoke(Request $request): MediaObject
0 commit comments