Codeigniter路由模式与前缀

时间:2017-12-27 07:47:26

标签: php codeigniter routes

我正在使用Codeigniter 3路由机制,我的应用程序是多分支,我尝试了像app.com/p/branch_id/controller/method/param1/param2/etc这样的谷歌邮件网址模式,但有些情况下它不需要前缀p/branch_id/管理员或全球设置。我认为我的规则并不有效。

$route['p/(:num)/(:any)'] = '$2';
$route['p/(:num)/(:any)/(:any)'] = '$2/$3';
$route['p/(:num)/(:any)/(:any)/(:any)'] = '$2/$3/$4';
$route['p/(:num)/(:any)/(:any)/(:any)/(:any)'] = '$2/$3/$4/$5';

I want achieve
/p/1/booking -> route to controller booking
/p/1/booking/create -> route to controller booking action create()
/p/1/booking/view/1 -> route to controller booking action view($id)

also same thing when user visit
/booking -> route to controller booking
/booking/create -> route to controller booking action create()
/booking/view/1 -> route to controller booking action view($id)

$route['p/(:num)/(:any)'] = '$2';仅仅路由索引控制器时,我如何为数字未知的参数编写规则。

1 个答案:

答案 0 :(得分:0)

我相信它会奏效。

$route['p/(:num)/(:any)'] = 'booking/action/$1/$2';
$route['p/(:num)/(:any)/create'] = 'booking/create/$1/$2';
$route['p/(:num)/(:any)/view/(:num)'] = 'booking/view/$1/$2/$3'; //$3 is record no.
  

加成

或者,如果您想将动作作为动态变量。

$route['p/(:num)/(:any)'] = 'booking/defaultAction/$1/$2';
$route['p/(:num)/(:any)/(:any)'] = 'booking/defaultAction/$1/$2'; // here in default action you need to add condition that if its create then execute create functionality. or if its view then execute view funcitonality