php - 自定义集合操作和IRI转换问题

标签 php symfony symfony4 api-platform.com

当我需要在路由中手动要求一个参数时,我遇到了 Api 平台和自定义集合操作的问题。

  1. 我的第一个需要是在这条路线上获取:query/userjob/[USER UUID] 并检索给定用户的所有作业的集合。
  2. 我的第二个需求是能够获取 query/userjob/[USER UUID]/[JOB UUID] 并检索给定用户的工作的详细信息。

说我没有 Api 资源或实体用户可能很重要,因此我排除了所有类型的子资源映射或查询。

所以,假设我有一个映射如下的 UserJob ApiResource:

App\Domain\User\Projection\UserJob:               
        itemOperations:
            get: 
                method: 'GET'
                path: '/userjob/{userId}/{jobId}'
                requirements:
                    userId: '%uuid_regex%'
                    jobId: '%uuid_regex%'
        collectionOperations:            
            get:
                method: 'GET'
                path: '/userjob/{userId}'
                requirements:
                    userId: '%uuid_regex%'
        attributes:
            route_prefix: "/query"

在类里面,我有:


final class UserJob
{
    public $id; //int Auto inc
    public $userId; //a UUID
    public $jobId; //a UUID

    public function __construct($userId, $jobId)
    {
        $this->userId = $userId;
        $this->jobId = $jobId;

    }

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

    public function getUserId()
    {
        return $this->userId;
    }

    public function getJobId()
    {
        return $this->jobId
    }   

我为这个类构建了一个自定义数据提供者,我在其中写了从 giver 参数(userId)获取资源的方法:

    public function getCollection(string $resourceClass, string $operationName = null)
    {
        $userId = $this->request->getCurrentRequest()->attributes->get('userId');
        return $this->repository->entityManager->getRepository($resourceClass)->findByUserId($userId);
    }

当我对 query/userjob/148e3200-f793-447b-bde8-af6b7b27372c 进行 GET 调用时,它会引发异常:

Unable to generate an IRI for App\Domain\User\Projection\UserJob

如果我更深入地调试,在 IRIConverter 类中,我发现原来的异常是从 Router 抛出的:

Some mandatory parameters are missing ("userId") to generate a URL for route "api_user_jobs_get_collection".

然而,如果我转储 $this->repository->entityManager->getRepository($resourceClass)->findByUserId($userId); 的结果,我正在寻找的所有元素for 很好地从数据库中获取。

所以我的直觉是 ApiPlatform 进程以某种方式无法构建我们通常可以在有效负载开头找到的集合 IRI,在我的例子中是 query/userjob/148e3200-f793-447b-bde8 -af6b7b27372c

在规范化或序列化过程中它失败了,因为我的自定义操作的“额外”参数(用户 UUID)没有传递给集合规范化器,iri 转换器类,所以它无法提供给路由缺少的参数以构建“api_user_jobs_get_collection”路由。

我在这里错过了什么?这是一个众所周知的问题,有一个我错过的现成解决方案吗?

或者我必须寻找:

  • 装饰 IRI 转换器?
  • 使用自定义标准化器?
  • 用复合 ID 做点什么?
  • 还有别的吗?

最佳答案

您的用例可能有更多解决方案,这取决于首选:

  1. 修饰 iri 转换器,因为您在集合中使用标识符,据我所知,API 平台不支持开箱即用。如果这样的 url 是您的 api 的样式,这是最佳选择。
  2. 使用具有自定义 url 样式的自定义 Controller 操作(文档:https://api-platform.com/docs/core/controllers/#creating-custom-operations-and-controllers),最好是在您的 api 中这是罕见的 url
  3. 将您的 ID 注释为您的 api 资源类中的标识符(文档:https://api-platform.com/docs/core/identifiers/#custom-identifier-normalizer)
    /**
     * @var Uuid
     * @ApiProperty(identifier=true)
     */
    public $code;
    
    但我还没有用多个 ID 尝试过,这可能只适用于项目 url。

您可以尝试使用自定义数据提供程序(文档:https://api-platform.com/docs/core/data-providers/)。这将需要按资源或全局(supports() 方法)完成,并且您需要以某种方式(正则表达式?)从 $context 数组中的 url 中提取 ids getCollection()getItem() 方法。但是由于 ApiPlatform 将尝试生成项目 iri,您最终可能仍会装饰 iri 转换器。

注意:在集合 url 中使用 id 可能会导致其他问题,例如 OpenAPI 文档生成。您可能会考虑,如果您想要的不是按“id”字段过滤集合,得到很好的支持,或者只检索“您的”项目的集合。这可以由您的数据提供者注入(inject)安全性来完成,或者如果有人使用了学说,则可以通过学说查询扩展来完成。

关于php - 自定义集合操作和IRI转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58040770/

相关文章:

javascript - ajax表单提交不起作用

php - Symfony2 使用 session 键和值创建对象

php - Symfony 4.0 中 FilesystemCache (SimpleCache) 的依赖注入(inject)

forms - Symfony 4 表单集合类型 : make FileType element required for new rows only

php - 返回新响应返回空数据

php - Ajax表单返回变量给php

php - Visual Studio Code 中自定义函数的参数建议

javascript - 在javascript block 中遍历twig数组

PHP MYSQL查询删除所有不在循环中的值

php - Symfony 3 - 无法加载类型表单类型