CodeIgniter Forums
URI segments with index() of a controller? - 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 segments with index() of a controller? (/showthread.php?tid=7882)



URI segments with index() of a controller? - El Forum - 04-25-2008

[eluser]inktri[/eluser]
I've got a User controller. I want users to be able to view their profiles with:
http://localhost/user/<username>

So I added this function into the User controller:
Code:
function index()
    {
        $username = $this->uri->segment(1);
        $users = $this->db->query('select * from users where username = "' . $username . '" limit 1');
        
        if ($users->num_rows > 0) {
            $user = $users->result();
            $data['user_info'] = $user[0];
            $this->load->view('user_view', $data);
        } else {
            $this->load->view('invalid_user_view');
        }
    }

however going to url http://localhost/user/<username> produces a 404 error...
if I rename the function above to view() and use segment(3), and direct my browser to http://localhost/user/view/<username> everything works fine. Anybody know what I'm doing wrong with the index() case?

Thanks


URI segments with index() of a controller? - El Forum - 04-25-2008

[eluser]wiredesignz[/eluser]
Yep. CI will try to locate the username as a method of the controller. You may like to consider _remap() instead. Or use routes. Good Luck and Welcome to the forums.


URI segments with index() of a controller? - El Forum - 04-25-2008

[eluser]inktri[/eluser]
thanks _remap() worked