phpsymfonyknppaginatorknppaginatorbundle

Implementation knpPaginator in symfony 6


Q A
Bundle version x.y.z
Symfony version 6.0.4
PHP version 8.1.6

Support Question

Hi,

I am asking for help in creating the pagination for the index. I've tried many ways and everyone has failed. Below I enclose the data for the project:

View MySql and componnets symfony

Controller to implement paginator

What I must change for it to work?

Code KnpPaginator

Error


Solution

  • I have this pagination implementation in my old project, look maybe u can find something for you.

    /**
         * @param EntityManagerInterface $entityManager
         * @param PaginatorService $paginatorService
         * @param Request $request
         * @return Response
         */
        #[Route('/service', name: 'service_index')]
        public function index(
            EntityManagerInterface $entityManager,
            PaginatorService $paginatorService,
            Request $request
        ): Response {
            return $this->render('service/index.html.twig', [
                'services' =>
                    $paginatorService->paginate($entityManager->getRepository(Service::class)->findAll(), $request)
            ]);
        }
    
    
    class PaginatorService
    {
        public function __construct(
            private PaginatorInterface $paginator,
        ) {
        }
    
        public function paginate($query, Request $request)
        {
            return $this->paginator->paginate(
                $query,
                $request->query->getInt('page', 1),
                15
            );
        }
    }
    
    <div class="row">
        <div class="col">
            <div class="pagination mt-3 d-flex justify-content-center">
                {{ knp_pagination_render(services, '/pagination/bootstrap_v5_pagination.html.twig') }}
            </div>
        </div>
    </div>