Welcome Guest, Not a member yet? Register   Sign In
The URL Structure of CodeIgniter
#1

[eluser]cinoob[/eluser]
Hi all,

I'm new to CodeIgniter and have a question about the structure of a URL. In the video tutorial the address to receive comments for particular blog post was something like www.example.com/index.php/blog/comments/1 where 1 was the id of the post for the comments. Setting the where statement for this was done using:

Code:
$this->db->where('id', $this->uri->segment(3));


I have a similar situation but with a url more similar to www.example.com/index.php/comments/1.

Using:
Code:
$this->db->where('id', $this->uri->segment(2));
doesn't work as going to the address www.example.com/index.php/comments/1 gives a 404 error as I have not created a function named 1.

Is there any way around this without changing the URL structure?
#2

[eluser]CI Coder[/eluser]
Because "comments" is your controller and 1 is supposed to be the function within that controller. The structure of the URL in CI is .../controller/function/segment3/segment4/etc.

I assume that you have the code in Comments::index() function. Try accessing the page like this: http://www.example.com/comments/index/1 and change "segment(2)" to "segment(3)"
#3

[eluser]cinoob[/eluser]
Thanks, I forgot I could use index. I guess I could use some url rewriting to tidy it up later but that works for now.
#4

[eluser]jedd[/eluser]
Hi cinoob and welcome to the CI forums (though in a few weeks, when you're a CI Guru, you might regret this choice of handle! Smile

There's a few things in here - as CI Coder has observed you can just use the index() function and have that in the URL.

From a 'bigger picture' perspective you might want have comments() as your controller, and show() as your function. Sticking with a class (noun) and method (verb) approach is usually pretty safe, and makes things easier to understand I reckon.

I'd also suggest you drop the ->segment() lookups and instead use the built-in feature of retrieving values from the URL in your controller. For example, hybridising your code and my suggestions above:
Code:
// in controller Comments
function  show  ( $id = FALSE )  {
    if ($id)
        $this->Your_model->lookup_thing($id);
    }

In Your_model:
Code:
function lookup_thing ($id)  {
    $this->db->where ('id', $id);
    $query  =  $this->db->get();
    if ($query->num_rows > 0)
       return $query->result();
    else
       return FALSE;
    }




Theme © iAndrew 2016 - Forum software by © MyBB