Search code examples
symfonyhateoasrepresentation

You cannot use a route without an url generator


i'm trying to implement the Hateoas with PaginatedRepresentation, like describe in documentation :

see bellow my controller :

$hateoas = HateoasBuilder::create()->build();
        $paginatedCollection = new PaginatedRepresentation(
            $collection,
            $this->generateUrl('getUsers'), // route
            array(), // route parameters
            1,       // page number
            2,      // limit
            11       // total pages
        );


        return new Response($hateoas->serialize($paginatedCollection, 'json'),
            200,
            array('Content-Type' => 'application/json'));

but i got this error : You cannot use a route without an url generator


Solution

  • replaced :

    $hateoas->serialize($paginatedCollection, 'json')
    

    by

    $this->get('serializer')->serialize($paginatedCollection, 'json')