ApiPlatform 中的 URL 错误

问题描述 投票:0回答:1

我已将 ApiPlatform 添加到我的 Symfony 项目中。我已经设置了一个自定义提供程序,因为数据位于外部 Web 服务中。

这是我的实体:

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;
use App\State\CustomerProcessor;
use App\State\CustomerProvider;

#[ApiResource]
#[Post(processor: CustomerProcessor::class)]
#[Get(provider: CustomerProvider::class)]
class Customer
{
    #[Id]
    public string $customerId;

}

这是我在提供程序中的

provide()
方法:

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
    {
        // create a Customer object to return
        return new Customer();
    }

我缺少 /api/customers/{id} 的路由定义,而且路由 /api/customers 设置为返回单个资源。

这是为什么?

symfony api-platform.com
1个回答
0
投票

要将 /customers 视为集合的端点,请装饰实体类:

#[ApiResource(
    operations: [
        new Get(provider: CustomerProvider::class),
        new GetCollection(provider: CustomerProvider::class)
    ]
)]
class Customer
{
 ...
}

要解决单个资源路径的问题,请将此装饰添加到标识符中:

#[ApiProperty(identifier: true)]
© www.soinside.com 2019 - 2024. All rights reserved.