这看起来很基本,但我无法理解它。
我正在尝试向控制器中的方法发送多个参数,如下所示:
http://localhost/ci/index.php/subjects/3/state
这是我尝试过的路线:
$route['subjects/(:num)'] = 'subjects/view/$1';
$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';
方法接受了2个参数:
public function view($slug, $id = null){
}
但我似乎得到了404.我怎么能让它工作?我需要view方法总是接受1个参数和可选的其他参数。
注意:我包含了网址助手。
答案 0 :(得分:37)
您的路线支架有问题,只需将其从{}更改为()括号将起作用
这
$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';
到
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
答案 1 :(得分:7)
始终维护您的路由规则
像
$route['subjects/(:num)/(:any)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3/$4';
$route['subjects/(:num)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3';
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
始终遵循此模式进行路由
如果你这样添加
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
$route['subjects/(:num)/(:any)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3/$4';
$route['subjects/(:num)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3';
然后每次第一个条件都是真的。
也请参阅此链接 - > codeigniter routing rules
答案 2 :(得分:2)
我曾尝试过这种URI模式
$route['(:any)'] = 'welcome/list1/$1';
$route['(:any)/(:num)'] = 'welcome/list1/$1/$2';
$route['(:any)/(:any)'] = 'welcome/list2/$1/$2';
$route['(:any)/(:any)/(:num)'] = 'welcome/list2/$1/$2/$3';
$route['(:any)/(:any)/(:any)'] = 'welcome/list3/$1/$2/$3';
但它没有用......所以我用正则表达式替换它
([a-z 0-9 -]+)
已取代(:any)
和
([0-9]+)
已取代(:num)
所以它变成了
$route['([a-z 0-9 -]+)'] = 'welcome/list1/$1';
$route['([a-z 0-9 -]+)/([0-9]+)'] = 'welcome/list1/$1/$2';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)'] = 'welcome/list2/$1/$2';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)/([0-9]+)'] = 'welcome/list2/$1/$2/$3';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)/([a-z 0-9 -]+)'] = 'welcome/list3/$1/$2/$3';
它对我有用:)