I'm a young developer and I recently discover Api-Platform to make a full rest api.
Currently, I'm following the doc, but I don't understand how to get all books of a person by example.
Here I am:
I have Book and Person entities auto generated, I just add a ManyToMany relation between them.
Then I have the following results:
GET api.platform.dev/app_dev.php/
{
@context: "/app_dev.php/contexts/Entrypoint",
@id: "/app_dev.php/",
@type: "Entrypoint",
person: "/app_dev.php/people",
book: "/app_dev.php/books"
}
GET api.platform.dev/app_dev.php/people/3
{
@context: "/app_dev.php/contexts/Person",
@id: "/app_dev.php/people/3",
@type: "http://schema.org/Person",
birthDate: null,
description: "test",
gender: "Femme",
name: "test",
url: null,
books: [
"/app_dev.php/books/1",
"/app_dev.php/books/4"
]
}
Here is my questions, How can I get in the second result an hypermedia to all books of the person, and What is the best option to get all books of a person ?
I have started with create my custom operation:
#services.yml
resource.person.item_operation.custom_get:
class: "Dunglas\ApiBundle\Api\Operation\Operation"
public: false
factory:
- "@api.operation_factory"
- "createItemOperation"
arguments:
- "@resource.person" # Resource
- ["GET"] # Methods
- "/people/{id}/books" # Path
- "AppBundle:Person:custom" # Controller
- "my_custom_route2" # Route name
- # Context (will be present in Hydra documentation)
"@type": "hydra:Operation"
"hydra:title": "A custom operation"
"returns": "xmls:string"
and
// PersonController.php
<?php
namespace AppBundle\Controller;
use Dunglas\ApiBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
class PersonController extends ResourceController
{
public function customAction(Request $request, $id)
{
return parent::getAction($request, $id);
}
}
result on GET api.platform.dev/app_dev.php/people/3/books is the same of the basic api.platform.dev/app_dev.php/people/3, normal I call the parent.
But now what is the best way to have something like this:
# GET api.platform.dev/app_dev.php/people/3/books
{
@context: "/app_dev.php/contexts/Book",
@id: "/app_dev.php/people/3/books",
@type: "hydra:PagedCollection",
hydra:totalItems: 2,
hydra:itemsPerPage: 30,
hydra:firstPage: "/app_dev.php/people/3/books",
hydra:lastPage: "/app_dev.php/people/3/books",
hydra:member: [
{
@id: "/app_dev.php/books/1",
@type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
},
{
@id: "/app_dev.php/books/2",
@type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
}
]
}
And when I get api.platform.dev/app_dev.php/people/3 add this IRI /app_dev.php/people/3/books
Thanks you for the help you could give me.