[symfony6] knp paginator

Résolu/Fermé
RomainGA Messages postés 109 Date d'inscription mardi 4 juillet 2017 Statut Membre Dernière intervention 20 avril 2024 - 15 août 2022 à 15:09
 Pitet - 16 août 2022 à 12:25

Bonjour,

<?php

namespace App\Controller;

use App\Repository\IngredientRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

class IngredientController extends AbstractController
{
    #[Route('/ingredient', name: 'app_ingredient')]
    public function index(IngredientRepository $repository, PaginatorInterface $paginator, Request $request): Response
    {
        $pagination = $paginator->paginate(
            $repository->findAll(),
            $request->query->getInt('page', 1), /*page number*/
            1 /*limit per page*/
        );

        $ingredients = $repository->findAll();
        return $this->render('pages/ingredient/index.html.twig', [
            'ingredients'=>$ingredients
        ]);
    }
}

A partir de la, je ne devrais avoir que 1 ingredient mais tout les ingredients s'affiche.

Que  faire ?

1 réponse

Bonjour,

Il faut passer la variable $pagination au lieu de $ingredients à ta vue.

Reprends les exemples d'utilisation du bundle pour comprendre comment l'utiliser : https://github.com/KnpLabs/KnpPaginatorBundle#user-content-usage-examples

0