Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routes configuration for named arguments in CakePHP

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.

like image 793
Muhammad Yasir Avatar asked Nov 14 '22 09:11

Muhammad Yasir


1 Answers

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'];
like image 50
Oscar Avatar answered Nov 29 '22 10:11

Oscar