Welcome Guest, Not a member yet? Register   Sign In
URL / Controler realted
#1

[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?
#2

[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 */ }
#3

[eluser]jcopling[/eluser]
Alternatively you could say:

Code:
class Services extends Controller{
    function Services() {
        parent::Controller();
    }
        
    function service_name($location=''){
        //do something with $location
    }

You can add as many url segments as you like so long you have a corresponding function parameter.
#4

[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 */ }
It only checks if segment 3 = 'locations', correct?

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 */ }
Is this correct?
#5

[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{
    function Service() {
        parent::Controller();
    }
        
    function service_name($location=''){
        switch($location){
            case 'location1':
                //do something
                break;
            case 'location2':
                //do something else
                break;
            case default:
                /* this one is especially important as you will want to know if the url segment was not passed at all */
                break;
        }
    }
}
#6

[eluser]xwero[/eluser]
if you have other words that are also valid you could do something like this
Code:
$thirdsegment = array('location', 'someword', 'anotherword');
$result = array_search($this->uri->segment(3),$thirdsegement);
if(is_numeric($result))
{
   // valid third section
   switch($result)
   {
     case 0: /* do something */; break;
     case 1: /* do something */; break;
     case 2: /* do something */; break;
   }
}
else
{
  // not valid third section
}

edit : the check for no third segment has to happen before you start checking for the segment value.
#7

[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='')
        {
            $data['pgtitle'] = 'Catholic Charities - Veterans Services';
            $data['crumb'] = 'services';
            $data['left_col'] = 'services';

            switch($location){
            case 'location1':
            $this->template->load('ccSecondaryPageTemplate', 'test/index1', $data);
                break;
            case 'location2':
            $this->template->load('ccSecondaryPageTemplate', 'test/index2', $data);
                break;
            case default:
            $this->template->load('ccSecondaryPageTemplate', 'services/veterans_services', $data);
                break;
        }

I'm going to try the other example and see what happens
#8

[eluser]tonanbarbarian[/eluser]
it is just default not case default
Code:
function veterans($location='')
        {
            $data['pgtitle'] = 'Catholic Charities - Veterans Services';
            $data['crumb'] = 'services';
            $data['left_col'] = 'services';

            switch($location){
            case 'location1':
            $this->template->load('ccSecondaryPageTemplate', 'test/index1', $data);
                break;
            case 'location2':
            $this->template->load('ccSecondaryPageTemplate', 'test/index2', $data);
                break;
            default:
            $this->template->load('ccSecondaryPageTemplate', 'services/veterans_services', $data);
                break;
        }
#9

[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 :-)
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB