44_
44_

Reputation: 72

Custom API Platform data provider not being found

I have created a custom data provider and corresponding entity. I have added dummy data for now since it's not working. The resource is available in the docs and the operation runs, but the endpoint always comes back with no data. My entity is here

<?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;
    }
}

and data provider is

<?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);
    }
}

I have tried every way of using the data provider service, autowired or explicitly configured. The endpoint works but never returns any data

Upvotes: 1

Views: 5021

Answers (1)

Nikita U.
Nikita U.

Reputation: 3618

Just change

('GET' === $operationName)

to

('get' === $operationName)

Upvotes: 1

Related Questions