URL / Controler realted |
[eluser]exodus7[/eluser]
I don't know how to get at this, but lets say for instance, I have a url of www.mydomain.com/index.php/services/service_name/locations I know that I would need to create a controller named "services" and a function inside the controller named "service_name", but what/how would I get the "locations" part of the URI to work? Does that makes sense, or am missing something?
[eluser]xwero[/eluser]
adding to the url isn't difficult. You can add all sorts of things after the method segment (service_name). In your view you can do something like following code when the url helper is loaded Code: <?php echo site_url('services/service_name/locations'); /* output : http://site.com/index.php/services/service_name/locations */ ?> To retrieve that segment you have to use the segment or the rsegment (routed segment) method from the uri library Code: if($this->uri->segment(3) == 'locations'){ /* do something */ }
[eluser]jcopling[/eluser]
Alternatively you could say: Code: class Services extends Controller{ You can add as many url segments as you like so long you have a corresponding function parameter.
[eluser]exodus7[/eluser]
xwero, Thanks for the quick reply. This is making more sense - In your example, Code: if($this->uri->segment(3) == 'locations'){ /* do something */ } So if I had more than one occurrence of the the same name for the 3rd URI segment, Im assuming I would have to verify the second & third segment to = specific values like so: Code: if($this->uri->segment(2) == 'service_name' && $this->uri->segment(3) == 'location'){ /* some code */ }
[eluser]jcopling[/eluser]
You would not have to check segment(2) against anything Code Igniter is already routing the page to the appropriate controller/function. All you would want to check would be the function parameter: Code: class Service extends Controller{
[eluser]xwero[/eluser]
if you have other words that are also valid you could do something like this Code: $thirdsegment = array('location', 'someword', 'anotherword'); edit : the check for no third segment has to happen before you start checking for the segment value.
[eluser]exodus7[/eluser]
I want to say thanks jcopling & xwero , you've shed lots of light on this and its making more sense. After trying jcopling's version, I'm getting an error "Parse error: syntax error, unexpected T_DEFAULT" It says the error occurred on the line that starts with "case default:" Code: function veterans($location='') I'm going to try the other example and see what happens
[eluser]tonanbarbarian[/eluser]
it is just default not case default Code: function veterans($location='')
[eluser]exodus7[/eluser]
tonanbarbarian, you are correct - it works seamlessly with no problems now - Thank you I see xwero's example to be efficient code if there are multiple options for the third segment. Does "case 0:", "case 1:" refer to the array? I appreciate the help all of you have given :-)
[eluser]Chris Newton[/eluser]
I prefer to use the uri_to_assoc function, rather than using the segment. This allows you to pass key=>value pairs and doesn't require that the variable you pass is always in the same URI segment; http://ellislab.com/codeigniter/user-gui...s/uri.html This allows you to use urls like; http://site.com/index.php/services/servi...ocations/1 http://site.com/index.php/services/servi...ocations/2 http://site.com/index.php/services/servi...ocations/1 and just look for the value of locations, rather than test for every possible location. |
Welcome Guest, Not a member yet? Register Sign In |