Passed arguments routing and custom ajax pagination url in cakephp

While working in my recent “charity project” i had to list all charities found in a particular region or state. I created a regions_controller and an index function in it to fetch and set records and index.ctp as a view page to display HTML results. As normal, it had to be a http://givebackindia/regions/index call to process the page, but, i wanted to look it like http://givebackindia/region/punjab in the browser address bar so i added a “route elements” routing in my app/config/routes.php file as below:
Router::connect("/region/:id/*",array('controller'=> 'regions','action' => 'index'),array('id'=>'[0-9a-z-]+'));
The routing above simply redirects all “/region/punjab” calls to “regions” controller’s “index” action (function). And to pass “punjab” as a route element i used “:id” placeholder and created an “id” named element in controller’s “$this->params” array (To learn more about routes configuration visit CakePHP’s routes configuration page).
Now, accessing $this->params['id'] in controller function gave me “punjab”, so i called $this->paginate(’Charity’) (with conditions for id) and had results alright. Also, in the index.ctp view i added url’=>array(’controller’=>’charities’, action’=>’index’) in to $paginator->options() method as an action url for the pagination helper functions.
Everything looked great. So what’s the problem? In fact, before using “route elements” i had tried passed arguments routing like this:
Router::connect("/region/:id/*",array('controller' =>'regions','action' => 'index'),array('pass'=>array('id')));
Everything was fine with it as well, but one thing which did not look good to me was http://givebackindia/regions/index/punjab/page:2 when one mouse over the pagination links. Although it was a ajax pagination, it would not have made much difference to the functioning of pages, it just did not look good to me! Ok. so i decided to eliminate the mouse over display of url.
Following the piece of regular expression which replaced the “not good looking url” with one i wanted to be there:
echo preg_replace('/href="[^"]+"/is', 'href="#paginate"',$paginator->prev("Previous Page"));
I replaced http://givebackindia/regions/index/punjab/page:2 with #paginate so it look http://givebackindia/region/punjab#paginate when mouseover which was quite pleasing for me. You may use this piece of regular expression to insert some javascript code or anything else inside your urls.
Thanks for your reading patience!