23

This seems really basic but i can't get the hang of it.

I'm trying to send more then one parameter to a method in the controller, like this :

http://localhost/ci/index.php/subjects/3/state

This is the routings i've tried :

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

the method accepted 2 paremeters :

public function view($slug, $id = null){

}

but i seem to get a 404. How can i get this to work? i need the view method to always accept 1 parameter and optional other parameters.

NOTE : I am including the url helper.

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142

3 Answers3

44

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';
umefarooq
  • 4,540
  • 1
  • 29
  • 38
  • Doing that got me stuck on an endless loops of refreshing so i guess something is wrong here... – eric.itzhak Nov 06 '12 at 07:54
  • Eric, are you still having an issue because this should work for your situation. Is there other custom routing going on? – Malachi Nov 06 '12 at 09:44
  • Ya i solved it, the problem was with URLS of ajax called i made that caused infinte loop but i fixed those. Thanks! – eric.itzhak Nov 06 '12 at 11:01
  • How can we pass multiple parameters for routing? – Aaru Dec 11 '13 at 06:41
  • for multiple parameter try this $route['subjects/(:any)'] = 'subject/view'; – umefarooq Dec 11 '13 at 07:06
  • how can i access the variables in controller? i tried using `($id = null) { print_r($id); print_r($_GET);die();` but both didnt return any value – guradio Apr 29 '20 at 13:31
  • these parameters are part of URL not URL query string, you can access them with CI uri library segment like $this->uri->segment(1); 1 is number of segment you want to access read more here https://codeigniter.com/userguide3/libraries/uri.html – umefarooq Apr 29 '20 at 22:59
10

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

  • how can i access the variables in controller? i tried using ($id = null) { print_r($id); print_r($_GET);die(); but both didnt return any value – guradio Apr 29 '20 at 13:36
3

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 :)

In order to access the variables in your controllers, you can assign any parameter in the function.

class Welcome extends CI_Controller {

    public function list($first, $second)
    {
        var_dump($first);
        var_dump($second);
    }
}
Ankur Singh
  • 161
  • 1
  • 6