php - Custom pagination route using CakePHP 2.3.1 -


i using cakephp 2.3.1 , have problem paginator component.

my goal pages like:

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

i have created follow route:

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

(as can see first route work handle page param)

now, handle page param correctly, added follow line regionscontroller's beforefilter.

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

because read paginator componenet ad ['named']['page'] instead of ['page'] directly.

first question:

is correct? need hack in beforefilter() ?


then, need show numbers of pagine using:

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

the problem here url created on link wrong.. point to:

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

i do not need urls these, need:

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

second question:

how force url want instead of controller/action/page:n format?

thanks!

this duplicate.

but, answering first question: depends. if you're sure url not going receive more parameters, routing can more like

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

and in "home" action do

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

i think beforefilter option better, since it's reusable , allows have params better organized.

now, article helped other similar question (and second question) resumes this

in home.ctp view (or whatever 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 usual

echo $this->paginator->prev('< prev'); echo $this->paginator->numbers(); echo $this->paginator->next('next >'); 

you'll have manually replace "page:" part of url , echo it.

of course, modify paginationhelper, wouldn't recommend because every cake update, have check if paginationhelper changed re-tweak it.

read article posted on other question in case you're not clear hope helps.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -