5

In my Cake application I have a controller "completed_projects". Its index action takes no arguments and lists some projects. Different pages can be accessed by example.com/completed_projects/index/page:23 etc.

I want to make the url's like this:

example.com/portfolio/page23

Obviously I need to make some routes for this. I've tried many of them like:

Router::connect('/portfolio/page:num', array('controller' => 'completed_projects', 'action' => 'index'), array('pass'=>'page:num', 'num'=>'[0-9]+'));

and also:

Router::connect('/portfolio/:page:num', array('controller' => 'completed_projects', 'action' => 'index'), array('named'=>'num', 'page'=>'page', 'num'=>'[0-9]+'));

I also tried modifying them again and again but none of them works well.

I am using CakePHP 1.3. Any help will be appreciated.

Muhammad Yasir
  • 646
  • 1
  • 4
  • 18

1 Answers1

1
Router::connect('/portfolio/page:page_num',
    array('controller'=>'completed_projects', 'action'=>'index'),
    array('page_num'=>'[\d]+')
);

In your controller, access page_num with:

$this->params['page_num'];
Oscar
  • 1,250
  • 13
  • 19
  • This passes page_num (like 23) to the action but I want that a named-parameter be passed like page:2 is passed. Thanks for considering to help anyway. – Muhammad Yasir Jul 17 '10 at 09:55
  • Thanks again Oscar. Could you please elaborate how this (or any) variable be used for pagination? Getting just a passed variable in controller is not requirement. I want it to be named argument (like page:33) so that it can be used for pagination. Is it even possible? – Muhammad Yasir Jul 21 '10 at 17:35
  • Ah, you meant that you want to change the way that the normal paginator URLs look like? – Oscar Jul 22 '10 at 08:13
  • You might want to take a look at this in that case, as far as I know there is no cleaner way to do this: http://stackoverflow.com/questions/1078558/cakephp-pagination-how-to-remove-page-from-url-for-better-seo-cleaner-url – Oscar Jul 22 '10 at 09:22