Search code examples
symfonyknppaginator

Symfony remove page parameter


I'm using knp_paginator on symfony to list the information. The information I'm listing generate lots of pages. The problem is when I filter my results.

Let's say I'm on page 3 the generated link is https://something.com/blabla?page=3 but since my filter doesn't generate 3 pages I get 0 results. If I change page on the URL i can see the filtered results. How can I make the page parameter disapear when I apply the filters with the controller?


Solution

  • I think answer for your question showed in an example of official docs:

      $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            10/*limit per page*/
        );
    
        // parameters to template
        return $this->render('AcmeMainBundle:Article:list.html.twig', array('pagination' => $pagination)); 
    

    In this example the default parameter will be first page with 10 max per page. If count records less than 10 it'll show 1 page with 7 records.