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
At this point, the entity is configured, but we still need to write the action
141
131
that handles the file upload.
@@ -167,7 +157,7 @@ final class CreateMediaObjectAction
167
157
}
168
158
```
169
159
170
-
## Resolving the File URL
160
+
### Resolving the File URL
171
161
172
162
Returning the plain file path on the filesystem where the file is stored is not useful for the client, which needs a
173
163
URL to work with.
@@ -216,7 +206,7 @@ final class MediaObjectNormalizer implements ContextAwareNormalizerInterface, No
216
206
}
217
207
```
218
208
219
-
## Making a Request to the `/media_objects` Endpoint
209
+
### Making a Request to the `/media_objects` Endpoint
220
210
221
211
Your `/media_objects` endpoint is now ready to receive a `POST` request with a
222
212
file. This endpoint accepts standard `multipart/form-data`-encoded data, but
@@ -231,7 +221,7 @@ your data, you will get a response looking like this:
231
221
}
232
222
```
233
223
234
-
## Accessing Your Media Objects Directly
224
+
### Accessing Your Media Objects Directly
235
225
236
226
You will need to modify your Caddyfile to allow the above `contentUrl` to be accessed directly. If you followed the above configuration for the VichUploaderBundle, that will be in `api/public/media`. Add your folder to the list of path matches, e.g. `|^/media/|`:
237
227
```caddyfile
@@ -244,7 +234,7 @@ You will need to modify your Caddyfile to allow the above `contentUrl` to be acc
244
234
...
245
235
```
246
236
247
-
## Linking a MediaObject Resource to Another Resource
237
+
###Linking a MediaObject Resource to Another Resource
248
238
249
239
We now need to update our `Book` resource, so that we can link a `MediaObject`
250
240
to serve as the book cover.
@@ -264,20 +254,18 @@ use Vich\UploaderBundle\Mapping\Annotation as Vich;
264
254
265
255
/**
266
256
* @ORM\Entity
267
-
* @ApiResource(iri="http://schema.org/Book")
268
257
*/
258
+
#[ApiResource(iri: 'http://schema.org/Book')]
269
259
class Book
270
260
{
271
261
// ...
272
262
273
263
/**
274
-
* @var MediaObject|null
275
-
*
276
264
* @ORM\ManyToOne(targetEntity=MediaObject::class)
277
265
* @ORM\JoinColumn(nullable=true)
278
-
* @ApiProperty(iri="http://schema.org/image")
279
266
*/
280
-
public $image;
267
+
#[ApiProperty(iri: 'http://schema.org/image')]
268
+
public ?MediaObject $image = null;
281
269
282
270
// ...
283
271
}
@@ -298,7 +286,7 @@ uploaded cover, you can have a nice illustrated book record!
298
286
Voilà! You can now send files to your API, and link them to any other resource
299
287
in your app.
300
288
301
-
## Testing
289
+
###Testing
302
290
303
291
To test your upload with `ApiTestCase`, you can write a method as below:
304
292
@@ -342,3 +330,154 @@ class MediaObjectTest extends ApiTestCase
342
330
}
343
331
}
344
332
```
333
+
334
+
## Uploading to an Existing Resource with its Fields
335
+
336
+
In this example, the file will be included in an existing resource (in our case: `Book`).
337
+
The file and the resource fields will be posted to the resource endpoint.
338
+
339
+
This example will use a custom `multipart/form-data` decoder to deserialize the resource instead of a custom controller.
340
+
341
+
### Configuring the Existing Resource Receiving the Uploaded File
342
+
343
+
The `Book` resource needs to be modified like this:
344
+
345
+
```php
346
+
<?php
347
+
// api/src/Entity/Book.php
348
+
namespace App\Entity;
349
+
350
+
use ApiPlatform\Core\Annotation\ApiProperty;
351
+
use ApiPlatform\Core\Annotation\ApiResource;
352
+
use Doctrine\ORM\Mapping as ORM;
353
+
use Symfony\Component\HttpFoundation\File\File;
354
+
use Symfony\Component\Serializer\Annotation\Groups;
355
+
use Vich\UploaderBundle\Mapping\Annotation as Vich;
0 commit comments