Custom URL for CakePHP Paginator helper
Well, I finally did it! After 3 hours of trial and error I finally got my custom URL’s for the Paginator Helper. Considering the amount of time it took me to achieve my desired URL format, I’ll share my solution with you. I don’t know if it’s the best solution out there (I didn’t find another) but it works for me.
As you know, CakePHP’s Paginator helper creates the URL’s with the page parameter appended at the end of the URL like so:
http://yoursite.com/controller/action/page:3
This URL does not work for me at all. I wanted to have a URL like this:
http://subdomain.mysite.com/category-3,
where 3 is the page number.
Let’s start by defining our necessary route rule so that when we get our desired URL, it will point to the right place and will not break pagination.
Router::connect('/:category-:page', array( 'controller'=> 'ads', 'action' => 'index' ), array( 'pass' => array( 'category', 'page' ), 'page' => '[0-9]+' ) );
Just paste the code above to your routes.php file and customize it to your needs. The next step is to rewrite the Paginator helper. Don’t worry, we won’t do much to it. Copy the helper class file from /cake/libs/views/helpers filder to your /app/views/helpers folder and open it.
Around the line 347 is the link method.
The last 4 lines of the method ar as follows:
$obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html'; $url = array_merge(array('page' => $this->current($model)), $url); $url = array_merge(Set::filter($url, true), array_intersect_key($url, array('plugin' => true))); return $this->{$obj}->link($title, $url, $options);
Edit them so they reflect the code below:
$url = array_merge(array('page' => $this->current($model)), $url); $url[] = $url[0].$url['page']; unset($url['page']); return $this->{$obj}->link($title, $url, $options);
Now we should modify the $paginator->options() array in our view to pass our category parameter. Here is how I did it. Paste this code before the $paginator->prev() method.
$paginator->options(array('url' => '/../../'.$value['Category']['slugname'].'-'));
It doesn’t look pretty but it gets the job done. I had to use relative paths otherwise cake’s internal mechanisms would break my whole URL format.
Play with it and if you come up with a better solution, I’m anxious to hear about it. I hope you find this method useful.
Happy baking.
Related posts
- How to paginate search results in CakePHP
- How to bake on Ubuntu using cakePHP
- How to switch between databases in CakePHP on the fly
- Alertpay integration with CakePHP
![[del.icio.us]](http://insanityville.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://insanityville.com/wp-content/plugins/bookmarkify/digg.png)
![[diigo]](http://insanityville.com/wp-content/plugins/bookmarkify/diigo.png)
![[Facebook]](http://insanityville.com/wp-content/plugins/bookmarkify/facebook.png)
![[Friendsite]](http://insanityville.com/wp-content/plugins/bookmarkify/friendsite.png)
![[LinkedIn]](http://insanityville.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Reddit]](http://insanityville.com/wp-content/plugins/bookmarkify/reddit.png)
![[Simpy]](http://insanityville.com/wp-content/plugins/bookmarkify/simpy.png)
![[Slashdot]](http://insanityville.com/wp-content/plugins/bookmarkify/slashdot.png)
![[StumbleUpon]](http://insanityville.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://insanityville.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://insanityville.com/wp-content/plugins/bookmarkify/email.png)



2 Responses
to “Custom URL for CakePHP Paginator helper”





very usefull thanks dude. you save me
Glad to help you.