如何在symfony中使用api-platform来编辑帖子数据?

我是 symfony 的新手,但主要是 api 平台。所以我查看了api-platform以了解更多信息。我读了很大部分,信息量很大。但我只是找不到一种方法来使用 openAPI 来获取编辑帖子数据。我也在使用学说和 SQLite

所以我所说的编辑帖子数据是什么意思。我安装了 api-platform 并让它工作(看起来像,this)。我可以在 url www.localhost/api 上使用我的 api-platform POST 数据,然后在 Role 下选择 POST,然后“try out”。我在学说中做了一个小表格,所以我发送给它的 JSON 是;

{
  "role": "string"
}

这样就可以了,它将数据添加到我的 SQLite 数据库中。但我想在 PHP 中编辑这些数据。但我就是不知道如何检索这个 JSON 并编辑我从 JSON 中获得的数据。

我所拥有的是;名为 Role 和 RoleRepository 的实体和存储库。我读过一个控制器不需要使用 api-platform 编辑数据。但我无法在任何地方找到如何编辑数据。

那么如何编辑从 JSON 中获得的数据呢?

编辑:如果您想知道我的实体角色是什么样的;

<?php
namespace AppEntity;

use AppRepositoryRoleRepository;
use ApiPlatformCoreAnnotationApiResource;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ApiResource()
 * @ORMEntity(repositoryClass=RoleRepository::class)
 */
class Role
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $role;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getRole(): ?string
    {
        return $this->role;
    }

    public function setRole(string $role): self
    {
        $this->role = $role;

        return $this;
    }
}

和 RoleRepository

<?php

namespace AppRepository;

use AppEntityRole;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use DoctrinePersistenceManagerRegistry;

/**
 * @method Role|null find($id, $lockMode = null, $lockVersion = null)
 * @method Role|null findOneBy(array $criteria, array $orderBy = null)
 * @method Role[]    findAll()
 * @method Role[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class RoleRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Role::class);
    }

    // /**
    //  * @return Role[] Returns an array of Role objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('r')
            ->andWhere('r.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('r.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Role
    {
        return $this->createQueryBuilder('r')
            ->andWhere('r.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

回答

Symfony 提供了所谓的内核事件,开发人员可以通过挂钩直接处理请求/响应,而无需编写特定的控制器或在数据到达控制器之前添加属性或更改某些内容。您可以在 HttpKernel 组件的文档中找到它。

API 平台通过挂钩这些事件来工作。您可以通过使用事件调度程序的 debug 命令来查看这一点:

bin/console debug:event-dispatcher

这应该打印出一个事件列表以及哪些监听器对事件起作用,并且使用 api-platform 你应该有一些新的。或者,您可以在 api-platform docs 中查找提供的事件侦听器列表。对您而言,ReadListener 和 WriteListener 可能是最相关的。您还可以查看 DeserializeListener,但这适用于所有 json 输入,因此您必须小心处理。

ReadListener 将使用数据提供程序从数据库中读取现有数据。当您想要编辑它时,您可以从这里获取现有实体。WriteListener 是将新的或编辑过的数据写入数据库的方式。这使用数据持久化程序。

如果您想手动更改编辑实体的工作方式,您可能需要编写自己的自定义数据持久器或自定义数据提供程序。

当您编写自己的数据持久化程序时,您需要确保它支持您的实体。您几乎可以从文档中复制示例,只需更改类名即可。然后 api-platform 将调用您的持久程序,您可以使用var_dump或 xdebug 来检查您在persist方法中获得的值。第一个参数$data应该是你的实体,在第二个参数中$context你应该得到更多信息,例如使用了哪个 http 动词,我认为还有解码的 json 数据。

编写自己的提供程序和持久程序可能有点棘手,通常不需要为每个实体都这样做(除非您不想在它们上使用标准的 getter 和 setter 方法)。相反,您还可以通过添加类似于@ApiResource注释的元数据信息来修改实体的处理方式,例如用于验证或更改类的(反)序列化方式。此外,持久器适用于已修改的实体,因此您的 json 数据已经过验证。没有什么能阻止您在那里进行额外的验证,但它可能是多余的,或者您可能认为为时已晚。简而言之,对于验证,您应该编写自己的验证约束或使用 Symfony 提供的约束。例如使用类似的东西IsTrue 约束的 在 isX/hasX/getX 方法上可能是解决验证需求的好方法。


以上是如何在symfony中使用api-platform来编辑帖子数据?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>