我已经创建了一个自定义数据提供者和相应的实体。我现在添加了虚拟数据,因为它无法正常工作。该资源在docs中可用,并且该操作可以运行,但是端点始终返回无数据。我的实体在这里
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* collectionOperations={"get"},
* itemOperations={"get"}
* )
*/
class Region
{
/**
* @ApiProperty(identifier=true)
*/
private $id;
private $name;
public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
和数据提供者是
<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Region;
use Doctrine\ORM\EntityManagerInterface;
class RegionDataProvider implements CollectionDataProviderInterface,
RestrictedDataProviderInterface
{
/**
* @param string $resourceClass
* @param string|null $operationName
*
* @return \Traversable
*/
public function getCollection(string $resourceClass, string $operationName = null):
\Traversable
{
yield new Region(1, 'Region 1');
yield new Region(2, 'Region 2');
yield new Region(3, 'Region 3');
yield new Region(4, 'Region 4');
}
/**
* @param string $resourceClass
* @param string|null $operationName
* @param array $context
*
* @return bool
*/
public function supports(string $resourceClass, string $operationName = null, array
$context = []): bool
{
return (Region::class === $resourceClass) && ('GET' === $operationName);
}
}
我已经尝试了所有使用数据提供者服务的方法,这些方法是自动装配的或显式配置的。端点有效,但从不返回任何数据
只需更改
('GET' === $operationName)
到
('get' === $operationName)