Kevin
Kevin

Reputation: 5082

FOSRestBundle and Pagerfanta

I'm trying to paginate a result with Pagerfanta

My Controller:

    /**
     * @Rest\Get(
     *     path="/users",
     *     name="get_users",
     * )
     *
     * @return View
     */
    public function users()
    {
        $pagers = $this->getDoctrine()->getManager()->getRepository(User::class)
            ->findAllWithPagination(10, 1);

        return $this->view($pagers->getCurrentPageResults(), Response::HTTP_OK);
    }

My UserRepository:

/**
 * @param int $limit
 * @param int $offset
 * @return Pagerfanta
 */
public function findAllWithPagination(int $limit = 10, int $offset = 1): Pagerfanta
{
    $qb = $this->createQueryBuilder('u')
        ->select('u')
    ;

    return $this->paginate($qb, $limit, $offset);
}

/**
 * @param QueryBuilder $qb
 * @param int $limit
 * @param int $offset
 * @return Pagerfanta
 */
protected function paginate(QueryBuilder $qb, int $limit = 10, int $offset = 1): Pagerfanta
{
    if (0 == $limit || 0 == $offset) {
        throw new \LogicException('$limit & $offset must be greater than 0.');
    }

    $pager = new Pagerfanta(new DoctrineORMAdapter($qb));
    $currentPage = ceil($offset + 1) / $limit;
    $pager->setCurrentPage($currentPage);
    $pager->setMaxPerPage($limit);

    return $pager;
}

I don't understand why I have a 404 response when I'm trying to access this resource. What did I do wrong?

"message": "Page Not Found", "class": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException", "file": "/var/www/html/model-backend/vendor/white-october/pagerfanta-bundle/EventListener/ConvertNotValidCurrentPageToNotFoundListener.php",

Upvotes: 1

Views: 491

Answers (0)

Related Questions