Table of Contents
Warning
|
Work in progress |
This library is split across multiple composer packages. Using them all is currently only recommended for Symfony projects using Doctrine entities. As some packages depend on each other, only three are needed to be required in your composer.json
. You can add them using the following command:
composer install demos-europe/edt-jsonapi demos-europe/edt-extra demos-europe/edt-dql
As the implementation of the routing controller and its actions is highly dependent on the architecture and practices of the using application, it is not covered by this library.
However, the following code shows a naïve implementation to handle get
requests, that may serve as a starting point.
In this example a single resource type Book
is supported to be accessed via get
requests and its authors can be included in the response.
#[Route(path: '/api/{resourceType}/{resourceId}', name: 'api_resource_get', methods: ['GET'])]
public function getAction(
Request $request
ValidatorInterface $validator,
EventDispatcherInterface $eventDispatcher,
Router $router,
): Response {
$manager = $this->createManager();
$getProcessor = $manager->createGetProcessor(
new \EDT\JsonApi\Requests\DefaultProcessorConfig($validator, $eventDispatcher, $router)
);
return $getProcessor->createResponse($request);
}
protected function createManager(): \EDT\JsonApi\Manager {
// create the manager
$manager = new \EDT\JsonApi\Manager();
// create and configure your resources type configurations
$bookConfig = new BookBasedResourceConfig($propertyFactory);
$authorConfig = new PersonBasedResourceConfig($propertyFactory);
$bookConfig->id->setReadableByPath();
$bookConfig->title->setReadableByPath();
$bookConfig->price->setReadableByPath();
$bookConfig->authors
->setRelationshipType($authorConfig)
->setReadableByPath(DefaultField::YES, DefaultInclude::YES);
$authorConfig->setTypeName('Author');
$authorConfig->id->setReadableByPath();
$authorConfig->fullName->setReadableByPath();
// finalize the configuration and add your resource types to the manager
$manager->registerGetableType($bookConfig->getType());
$manager->registerType($authorConfig->getType());
return $manager;
}