Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...