Practical Examples Using URI Segments For URL's - 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: Practical Examples Using URI Segments For URL's (/showthread.php?tid=35335) |
Practical Examples Using URI Segments For URL's - El Forum - 10-26-2010 [eluser]mdvaldosta[/eluser] I feel stupid for asking this, but I just can't find any practical examples of structuring a controller's functions to work with URI segments. Wouldn't this be better than routing them? Does anyone have any practical examples for using this very basic CI feature? I'm used to using plain old GET. What specifically I'm after is creating a /controller/function/parameter url and working with it seemlessly in a "best practice" sort of way through the controller and views. Practical Examples Using URI Segments For URL's - El Forum - 10-26-2010 [eluser]techgnome[/eluser] OK... so let's say I have a news controller... and I want to view a specific article... So I create a news controller, with an article method, that takes one parameter. Code: class News extends Controller { Now my urls look like {base url}/news/article/123 Does that help? -tg Practical Examples Using URI Segments For URL's - El Forum - 10-26-2010 [eluser]n0xie[/eluser] To expand on the example: Code: class News extends Controller { Practical Examples Using URI Segments For URL's - El Forum - 10-26-2010 [eluser]mdvaldosta[/eluser] Say there were multiple parameters, but I also want the function to take zero parameters... Is the best way to do that just setting the variables to false and checking for that? Practical Examples Using URI Segments For URL's - El Forum - 10-26-2010 [eluser]tonanbarbarian[/eluser] rather than having parameters on the method, you can use the URI object to get the segments of the uri. this allows for a flexible number of parameters Code: $segments = $this->uri->uri_to_assoc(2); so the urls could be {base url}/news/article/id/123/format/print $segments would have the following id=>123 format=>print |