Monday, May 3, 2010

Cake PHP Pagination - SEO friendly URL

Today I was searching for SEO friendly Cake PHP Pagination URL.
I found the following link:-
http://www.sakic.net/blog/changing-cakephp-pagination-urls

But there was not much explanation about handling sort and pagination numbers.
I tried some modifications of the code given, and finally........ I got it!

Here's it:-

Controller-


Before cleaning URL:-
function index() {
$data = $this->paginate('User'); // User is the model name

$this->set('data', $data);

}


After cleaning URL:-
function index($page='', $sort='', $dir = '') {
if (is_numeric($page)) {
$this->passedArgs['page'] = $page;
$this->passedArgs['sort'] = $sort;

$this->passedArgs['direction'] = $dir;
}
$data = $this->paginate('User'); // User is the model name

$this->set('data', $data);

}


View-

Before cleaning URL:-
echo $paginator->sort('First name', 'first_name', array('class'=> $paginator->sortDir())) //sort
if ($paginator->counter('%pages%') > 1):
echo $paginator->numbers();
echo $paginator->prev('previous', null, null, array('class' => 'disabled'));
echo $paginator->next('next', null, null, array('class' => 'disabled'));
endif;

After cleaning URL:-

$replace_arr = array("page:", "sort:", "direction:"); // strings to be replaced from the pagination links

echo str_replace($replace_arr, '', $paginator->sort('First Name', 'first_name', array('class'=> $paginator->sortDir()))); //sort

if ($paginator->counter('%pages%') > 1):
$prev_link = str_replace($replace_arr, '', $paginator->prev('previous', null, null, array('class' => 'disabled')));
$prev_link = preg_replace('/\/1"/', '"', $prev_link);
$next_link = str_replace($replace_arr, '', $paginator->next('next', null, null, array('class' => 'disabled')));
echo preg_replace('/\/1"/', '"', str_replace($replace_arr, '',$paginator->numbers()));
echo $prev_link;
echo $next_link;
endif;

No comments:

Post a Comment