Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter routing segments/params

$route['segment1/(:any)/(:any)'] = "myController/$1/$2";

in this case, I will have to link segment1/someMethod/someParam, but what if I don't have params in the url, i will need to write this:

$route['segment1/(:any)'] = "myController/$1";
$route['segment1/(:any)/(:any)'] = "myController/$1/$2";

thus the both cases will now work, so the question is: Can I write those 2 lines of code in one shot?

like image 259
Hello Avatar asked Aug 07 '26 08:08

Hello


1 Answers

this:

$route['segment1/(:any)'] = "myController/$1";
$route['segment1/(:any)/(:any)'] = "myController/$1/$2";

is duplicating,they do the same thing, use just one of them, i suggest

$route['segment1/(:any)'] = "myController/$1";

hope to be clear, when using this "myController/$1" you are saying everything following myController/ should be routed, and it works also if no $1 params exists.

Definitely use just one of them and don't scare about not having params, it works like a charm ;)

like image 190
itsme Avatar answered Aug 10 '26 01:08

itsme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!