How to route 2 parameters to a controller?

早过忘川 提交于 2019-12-03 05:38:05

you have problem with your route brackets just change it from {} to () brackets will work

from

$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';

to

$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';

Always maintain your routing rules

like

$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';

always follow this pattern for routing

if you add like this

$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';

then always first condition will be true every time.

also refer this link --> codeigniter routing rules

I once tried this URI pattern

$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';

but it didnt worked ... so I replaced it with regular expression

([a-z 0-9 -]+) replaced (:any) and ([0-9]+) replaced (:num)

so it became

$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';

And it worked for me :)

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