简体   繁体   中英

Pagination for two different actions in the same controller

I have an action called index and another one called manage , both in PostsController . I want to implement pagination for both, and I've set up this class attribute:

public $paginate = array(
    'limit' => 10,
    'order' => array(
        'Post.created' => 'desc'
    )
);

then I'm using the pagination in my index action like so: $this->set('posts', $this->paginate('Post'));

This results in a URL like so: http://dev/posts/page:2 which is fine.

However, when I try to use pagination in my manage action just like I did with index ( $this->set('posts', $this->paginate('Post')); ), the pagination links on my view redirect to the URL above, rather than the manage action.

Basically, Cake is getting confused because I'm using pagination twice in the same controller and it's redirecting both to the same URL. How can I make sure that the pagination for the manage action works properly?

Do the followings.

<?php
class PostsController extends AppController
{
    var $name = 'Posts';
    public $paginate = array
    (
        'limit' => 10,
        'order' => array
        (
            'Post.created' => 'desc'
        )
    );

    function index()
    {
        $this->Post->recursive = 0;
        $this->set('posts', $this->paginate('Post'));
    }

    function manage()
    {
        $this->Post->recursive = 0;
        $this->set('posts', $this->paginate('Post'));
    }
}

make changes in view file as defined below.

index.ctp

$options = array
(
    'url'=> array
    (
        'controller' => 'posts', 
        'action' => 'index'
     )
);
$paginator->options($options);

manage.ctp

$options = array
(
    'url'=> array
    (
        'controller' => 'posts', 
        'action' => 'manage'
     )
);
$paginator->options($options);

And you are done.

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