简体   繁体   中英

symfony pagination multiple in same page

I would add several pagination button on the same page. Here is my page:

https://i.stack.imgur.com/IV7xy.jpg

Currently I use knppaginator like this:

$paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $demandes,
        $request->query->getInt('page', 1),
        5
    );

I try to use knppaginator in this way:

$paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $demandes,
        $request->query->getInt('page', 1),
        5
    );
    $paginator  = $this->get('knp_paginator');
    $paginationF = $paginator->paginate(
        $forumTopics,
        $request->query->getInt('page', 1),
        5
    );

But it does not work. When I change page with one it changes the page of the other.

Thank you.

I think it is because you use same "page" parameter for both paginations. Try this

$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
    $demandes,
    $request->query->getInt('page', 1),
    5
);

$paginationF = $paginator->paginate(
        $forumTopics,
        $request->query->getInt('otherPage', 1),
        5,
       ['pageParameterName' => 'otherPage']
    );

This is because, per default, all services are shared. This means that each time you retrieve the service, you'll get the same instance — see reference .

But that can be changed in the definition of your service if you flag it as not shared, as the documentation is proposing it:

# app/config/services.yml
services:
    app.some_not_shared_service:
        class: ...
        shared: false
        # ...

In you case your service is called knp_paginator and coming from their bundle.

So you just have to change your own app/config/services.yml to make their service non shared:

# app/config/services.yml
services:
    knp_paginator:
        shared: false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM