Search code examples
phpsymfonypaginationknppaginator

Paginate in KnpPager not work


I have a problem with my paginate. My route :

show_product_category:
path:     /{id}/{name}
defaults: { _controller: ShopDesktopBundle:Category:showCategory}
requirements:
    id:  \d+
    _method:  GET

In my controller :

    $aProducts          = $repositoryProduct->getProductsOrderByDateDesc($id);
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $aProducts,
        $this->get('request')->query->get('page', 1),
        3
    );
    return $this->render('ShopDesktopBundle:Category:category.html.twig',array(
        'pagination'        => $pagination
    ));

I view :

<div class="navigation">
     {{ knp_pagination_render(pagination) }}
</div>

It's calculate normal the count of products and in view I see : 1 2 3 >. But when I tried to get the page 2 in url it's send : ?page=2 but the list of products not change.


Solution

  • you missed to configure the page parameter and process it in your controller:

    use this route:

    show_product_category:
        path:     /{id}/{name}/{page}
        defaults:
            _controller: ShopDesktopBundle:Category:showCategory
            page: 1
        requirements:
            id:  \d+
            page: \d+
        methods: [GET]
    

    and in controller:

    public function showCategoryAction($id, $name, $page)
    {
        //your code.....
        $pagination = $paginator->paginate(
        $aProducts,
        $page,
        3
        );
        // your code...
    }