search

Home  >  Q&A  >  body text

Implementing knpPaginator in symfony 6

ask one
Bundled Version x.y.z
Symfony version 6.0.4
PHP version 8.1.6

Support Issues

Hi,

I am asking for help creating pagination for the index. I tried many methods but failed with everyone. Below I attach the data for this project:

View MySql and symfony components

Implementing the controller of the pager

What do I have to change to make it work?

CodeKnpPaginator

mistake

P粉982054449P粉982054449311 days ago382

reply all(1)I'll reply

  • P粉720716934

    P粉7207169342024-02-22 09:00:08

    I have this pagination implementation in my old project, take a look maybe you can find something that works 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
            );
        }
    }
    
    

    reply
    0
  • Cancelreply