CakePHPCakePHP framework provides a great pagination helper but its default URLs aren't that great. By default it generates URLs that look like: http://domain/news/page:2. I spent a long time looking for a way to skip "page:" part in URLs and in links generated by the helper, or even just change the string "page" to something else. It showed up that there's no easy way to configure the paginator that way. So I came up with a workaround that will change your CakePHP pagination URLs to: http://domain/news/2

Assuming that you already created pagination on your site according to CakePHP instructions we will start making a few changes. We begin with the view where we want to modify automatically generated links into the new ones:

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

This will take out "page:" part from the prev/next links and make them only contain the page number instead. Also, we replace  http://domain/news/1 with only http://domain/news in order to avoid alias URLs and penalties for duplicate content by search engines. Be sure to apply the same method to the rest of your links such as those generated by numbers() method etc., if you are using them. Now we need to make sure that the Cake makes correct interpretation of the passed argument. In our router file we add something like:

Router::connect('/news/*', array('controller' => 'news', 'action' => 'display'));

This will forward our pagination links to news controller and its display() method. Our page number parameter will be passed as argument to this method. Let's take a look at the code in news_controller.php:

public function display($option='') {
	if (is_numeric($option)) {
		$this->passedArgs['page'] = $option;
	$items = $this->paginate('News');

As we can see we take the passed argument and assign it to the passedArgs array under the 'page' key where all named URL parameters are held. This way the paginator will understand that we are on the page passed by our URL. That's it. If you've done it correctly you now have nice pagination URLs like: http://domain/news
instead of:
http://domain/news/page:1 (duplicate)

Emir Sakic Emir is Electrical Engineer turned web developer, co-founder of Joomla and a former member of the core development teams of Joomla and its predecessor Mambo.

Latest Comments

Re: Changing CakePHP pagination URLs « Sakic.Net blog Glad to be here! Such a nice post about...
Re: Domain checker module « Sakic.Net blog awesome work done. i hope your module is used...
Re: Migrating data from SMF to Kunena Hi everyone,If you had got any fraud, scam with...
Re: Migrating data from SMF to Kunena Hello! Emir, thank you for the article. I would...
Re: Migrating data from SMF to Kunena hello all,nice place to be here, i see nice...

About us

We provide high quality Joomla components created by a co-founder and original core developer of Joomla. For over a decade, our products have been used by more than 20.000 webmasters around the world.

Stay in touch