I'm currently developping some REST web services using Hateoas and I'd like to implement pagination for some longer list display.
Note : Database retrieve logic isn't implemented yet
This is my controller :
use Hateoas\Representation\PaginatedRepresentation;
use Hateoas\Representation\CollectionRepresentation;
/**
* @Rest\View(serializerGroups={"details"})
* @Doc\ApiDoc(
* section="User",
* resource=true,
* description="Get all catalogs accessible by a User",
* requirements={
* {
* "name"="id",
* "dataType"="integer",
* "requirement"="\d+",
* "description"="The id of the user from which to retrieve"
* }
* },
* output={
* "class"="\CatalogV2",
* "groups"={"details"}
* }
* )
*/
public function getUserLicencesAction($id, $page = 1, $limit = 10) {
$service_rustine = $this->container->get('rustine_core.link');
// Get User corresponding to id
$user = $service_rustine->getUser($id);
// Get licences
$licences = $user->getLicencesRight();
$offset = ($page - 1) * $limit;
$pages = (int)ceil(count($licences) / $limit);
$collection = new CollectionRepresentation(
array_slice($licences, $offset, $page * $limit),
'licences',
'licences',
new Exclusion(array("details"))
);
$paginated = new PaginatedRepresentation(
$collection,
'get_user_licences',
array("id" => $id),
$page,
$limit,
$pages
);
// JSON output
return $paginated;
}
The error I keep having is :
"Some mandatory parameters are missing ("id") to generate a URL for route "get_user_licences"
The documentation isn't very clear about route parameters and I can't find any example using a non empty array.
The routeparam id given in the parameters array is always ignored in the UrlGenerator. I've tried array($id) but it isn't working either.
When I try, in the same controller to generate the route with like this, there is no problem :
$this->get('router')->generate('get_user_licences', array('id' => $id));
Thank you for your help !
I have found the problem : There was actually a YML config file redefining the Hateoas\Representation\PaginatedRepresentation metadata ... The expressions used for the parameters in the route definitions were incorrect. For the "next" link for example I had :
expr(object.getPage() + 1)
instead of the
expr(object.getParameters(object.getPage() + 1))
Maybe this will help someone one day !