Paginate in KnpPager not work

旧城冷巷雨未停 提交于 2019-12-25 18:54:15

问题


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.


回答1:


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...
}


来源:https://stackoverflow.com/questions/29301112/paginate-in-knppager-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!