Codeigniter Routing Unlimited Paramaters

这一生的挚爱 提交于 2019-12-19 11:42:38

问题


I currently have this in my CodeIgniter routes file. It maps anything with a URI api/controller/function to controller/api_function.

$route['api/(:any)/(:any)/(:any)/(:any)/(:any)/(:any)'] = '$1/api_$2/$3/$4/$5/$6';
$route['api/(:any)/(:any)/(:any)/(:any)/(:any)'] = '$1/api_$2/$3/$4/$5';
$route['api/(:any)/(:any)/(:any)/(:any)'] = '$1/api_$2/$3/$4';
$route['api/(:any)/(:any)/(:any)'] = '$1/api_$2/$3';
$route['api/(:any)/(:any)'] = '$1/api_$2';

As you can see, this isn't very efficient. I need a different routes line depending on the number of parameters supplied. Is there a way to automatically pass all parameters along? So hypothetically, I'm looking for something like...

$route['api/(:any)/(:any)/unlimited parameters'] = '$1/api_$2/unlimited parameters';

Thanks!


回答1:


Try this:

$route['api/([^/]*)/([^/]*)/(.*)'] = '$1/api_$2/$3';

It basically checks for two segments (any character but a slash), then anything after that gets appended as parameters to your controller function.

This wouldn't match for routes with NO parameters, but it's not hard to do if that's a case you need to handle.




回答2:


I believe just this would do:

$route['api/(:any)/(:any)'] = '$1/api_$2';
$route['api/(:any)/(:any)/(:any)'] = '$1/api_$2/$3';



回答3:


This Works perfectly,

$route['api/(.*)']='api/$1';


来源:https://stackoverflow.com/questions/9455374/codeigniter-routing-unlimited-paramaters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!