sajad torkamani

In a nutshell

State providers are classes that retrieve data exposed by your API. They’re a new feature of API Platform 3 and replace the data providers from API Platform 2.

An example state provider

1. Create a state provider

Assuming you have the Symfony MakerBundle installed, you can run the below to quickly create a custom provider:

bin/console make:state-provider

An example provider looks like this:

<?php

namespace App\State;

use App\Entity\BlogPost;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;

final class BlogPostProvider implements ProviderInterface
{
    /**
     * {@inheritDoc}
     */
    public function provide(Operation $operation, array $uriVariables = [], array $context = [])
    {
        return new BlogPost($uriVariables['id']);
    }
}

2. Configure operation to use state provider

<?php

namespace App\Entity;

use ApiPlatform\Metadata\Get;
use App\State\BlogPostProvider;

#[Get(provider: BlogPostProvider::class)]
class BlogPost {}

Sources