// src/App/ApiResource.php
namespace App\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use App\State\BookProvider;
#[ApiResource(provider: BookProvider::class)]
class Book
{
public string $id;
}
// src/App/State.php
namespace App\State;
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\Book;
final class BookProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|object|null
{
if ($operation instanceof CollectionOperationInterface) {
$book = new Book();
$book->id = '1';
/* $book2 = new Book();
* $book2->id = '2'; */
return [$book/* $book2 */];
}
$book = new Book();
$book->id = $uriVariables['id'];
return $book;
}
}
// src/App/Playground.php
namespace App\Playground;
use Symfony\Component\HttpFoundation\Request;
function request(): Request
{
return Request::create('/books.jsonld', 'GET');
}
Made with love by
Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.
Learn more