Custom pagination route using CakePHP 2.3.1

2019-07-21 19:58发布

问题:

I am using cakephp 2.3.1 and I have problem with Paginator Component.

My goal are pages like:

example.com/abruzzo
example.com/abruzzo/2
example.com/abruzzo/3

I have created the follow route:

Router::connect('/:regione/:page', array('controller'=>'regions','action'=>'home'), array('page' =>'[0-9]+'));
Router::connect('/:regione', array('controller' => 'regions', 'action' => 'home'));  

(as you can see the first route work handle the page param)

Now, to handle the page param correctly, I added the follow line to RegionsController's beforeFilter.

public function beforeFilter()
{        
    $this->request->params['named']['page'] = (isset($this->request->params['page'])) ? $this->request->params['page'] : 1;
}

because I read the Paginator componenet will look ad ['named']['page'] instead of ['page'] directly.

FIRST QUESTION:

Is this correct? do I really need this hack in beforeFilter() ?


Then, I Need to show the numbers of the pagine using:

<?php echo $this->Paginator->numbers(); ?>

The problem here is that the URL created on the link are wrong.. they point to:

example.com/regions/home/page:2
example.com/regions/home/page:3
etc...

I do not need URLs like these, I need:

example.com/abruzzo
example.com/abruzzo/2
example.com/abruzzo/3

SECOND QUESTION:

How could force the url i want instead of controller/action/page:N format?

Thanks!

回答1:

This is probably a duplicate.

But, answering your first question: it depends. If you're sure your url is not going to receive more parameters, then your routing can be more like

Router::connect('/:regione/*', array('controller'=>'regions','action'=>'home'));

and in the "home" action do

public function home($pageOption=null) {
    if (is_numeric($option)) {
        $this->passedArgs['page'] = $pageOption;
    }
    $items = $this->paginate('Home');
    ...
}

I think the beforeFilter option is better, since it's reusable and allows you to have your params better organized.

Now, the article that helped the other similar question (and your second question) resumes to this

In home.ctp view (or whatever you like)

$prev_link = str_replace('page:', '', $this->Paginator->prev('« Prev'));
$prev_link = preg_replace('/\/1"/', '"', $prev_link);
$next_link = str_replace('page:', '', $this->Paginatornext('Next »'));
echo $prev_link;
echo $next_link;

So, instead of the usual

echo $this->Paginator->prev('< Prev');
echo $this->Paginator->numbers();
echo $this->Paginator->next('Next >');

you'll have to manually replace the "page:" part of the url and then echo it.

Of course, you could always modify the PaginationHelper, but I wouldn't recommend that because for every cake update, you have to check if the PaginationHelper was changed an then re-tweak it.

Read the article posted on the other question in case you're not clear about something Hope it helps.



回答2:

I think this is what you are looking for

https://github.com/hashmode/Cakephp-Beauty-Paginator