我将 Vich Uploader Bundle 与 API 平台结合使用来存储文件。我已按照官方文档https://api-platform.com/docs/core/file-upload/#handling-file-upload 中的说明进行操作
除了我想包含 PUT 调用来替换现有的 MediaObject 实体外,一切都运行良好。我的尝试如下:
我创建了一个自定义操作并将其映射到实体中的 PUT 方法,如下所示:
<?php
// api/src/Entity/MediaObject.php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\CreateMediaObject;
use App\Controller\EditMediaObject;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ApiResource(
*
* ...
*
* itemOperations={
* "get",
* "put"={
* "controller"=EditMediaObject::class,
* "deserialize"=false,
* "validation_groups"={"Default", "media_object_create"},
* "swagger_context"={
* "consumes"={
* "multipart/form-data",
* },
* "parameters"={
* {
* "in"="formData",
* "name"="file",
* "type"="file",
* "description"="The file to upload",
* },
* },
* },
* },
* "delete"
* },
* )
* @Vich\Uploadable
*/
class MediaObject
{
/**
* @var int|null
*
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @ORM\Id
*/
慕桂英4014372
POPMUISE
largeQ