CodeIgniter Forums
URI Routing, How to pass ID into method? - 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, How to pass ID into method? (/showthread.php?tid=50263)



URI Routing, How to pass ID into method? - El Forum - 03-20-2012

[eluser]TomRPS[/eluser]
The docs show this example route:
Code:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";

I have been trying to use this example to test how to get an ID into a method. So far nothing has worked.
In the example it has "$1" which I assume is supposed to be the ID. Is this correct and does it have to be $1 or can it be something else?

What do I put in my method's parameter? In the case of the above route a request to http://mysite.com/product/4323 would be routed to catalog.php and product_lookup_by_id method

So if I have the method:

Code:
public function product_lookup_by_id($id)
{
    echo $id;
}

Does the name of the parameter ($id in this case) matter? If so what should it be. When I use the code above all I get is a blank page.


URI Routing, How to pass ID into method? - El Forum - 03-20-2012

[eluser]hsn22[/eluser]
i'm pretty new to CI myself, so this may not be the optimal solution/answer.

try to get the id through this method:

Code:
$this->uri->rsegment(n)

where n would be 3 for your case.