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.

1 Answer 1

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'];
4
  • 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. Jul 17, 2010 at 9: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? Jul 21, 2010 at 17:35
  • Ah, you meant that you want to change the way that the normal paginator URLs look like?
    – Oscar
    Jul 22, 2010 at 8: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: stackoverflow.com/questions/1078558/…
    – Oscar
    Jul 22, 2010 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.