CodeIgniter Forums
URI Routing with more than one variable? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: URI Routing with more than one variable? (/showthread.php?tid=15369)



URI Routing with more than one variable? - El Forum - 02-02-2009

[eluser]juddmuir[/eluser]
Hi,

Is it possible to set up a routing rule that took e.g.
example.com/products/category_id/product_id
and passed BOTH the category_id and the product_id to whatever function?

In the User Guide, there's an example for only one variable:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";

Thanks in advance for any replies.


URI Routing with more than one variable? - El Forum - 02-02-2009

[eluser]Lukifer[/eluser]
I haven't tested it myself, but it works much like regular expressions, so you should be able to use:

Code:
$route['product/(:num)/(:num)'] = "products/$1/$2";

If that doesn't work, the normal regex version should:

Code:
$route['product/([0-9]+)/([0-9]+)'] = "products/$1/$2";



URI Routing with more than one variable? - El Forum - 02-02-2009

[eluser]sheldonnbbaker[/eluser]
Just add the rule in your routing config (with regular expressions or whatever) and then you can grab the variables with uri->rsegments().


URI Routing with more than one variable? - El Forum - 02-03-2009

[eluser]juddmuir[/eluser]
Thanks for your replies!
I used Lukifer's first option, worked a charm :-)