Question

I'm using codeigniter at the moment. I am currently able to view a persons profile by the using /user/profile/profile_id but I want to make it so that a user can just navigate to a profile using /username to make it simpler.

How would I go about doing this I'm not sure where to start?

class User extends CI_Controller{  

public function index(){

    if($this->session->userdata('is_logged_in')){
        redirect('user/profile');
    }

}

public function profile(){

    $profile_id = $this->uri->segment(3);
    $ip = $this->session->userdata('ip_address');
    $curr_user = $this->session->userdata('id');

    $data['profile'] = $this->db->get_where('users', array('id' => $profile_id))->row_array();  
    $data['followers'] = $this->db->get_where('followers', array('following_id' => $profile_id))->num_rows();       
    $data['following'] = $this->db->get_where('followers', array('follower_id' => $profile_id))->num_rows();
    $data['doesFollow'] = $this->db->get_where('followers', array('follower_id' => $curr_user, 'following_id' => $profile_id))->num_rows();
    $data['posts'] = $this->db->get_where('posts', array('user_id' => $profile_id))->result_array();        

    $data['main_content'] = 'profile';  
    $this->load->view('template', $data);   

    $this->get_profile_view($profile_id, $ip, $curr_user);  

}

} 

routes.php

$route['default_controller'] = "signin";
$route['404_override'] = '';
Was it helpful?

Solution

I take it that the functionality you're trying to achieve is the following:

  1. If the user navigates to http://example.com/(route) and route is mapped by a controller, then display that controller.
  2. If the user navigates to http://example.com/(route) and route is not mapped by any controller, then there is a chance the route is a username, and thus:
    1. If the route is a username, display that person's route.
    2. If the route is not a username, display a 404 page.

So the plan here is to create a custom 404 handler that checks if the provided route is a username, or else, it displays a 404 page.

The first thing we need to do is to set our custom 404 controller:

$route['404_override'] = 'profile';

We then create our custom 404 controller:

class Profile extends CI_Controller {
    public function __construct() {
            parent::__construct(); 
    }

    public function index()
    {
        $username = $this->uri_segment(1);

        if (empty($username)) {
            $this->displayPageNotFound();
        }

        $this->load->model('muser');

        // Check if parameter is not a valid username.
        if (!$this->muser->checkIfUsername($username)) {
            $this->displayPageNotFound();
        } else {
            // Load data for user profile.
            $ip = $this->session->userdata('ip_address');
            $curr_user = $this->session->userdata('id');

            $data['profile'] = $this->db->get_where('users', array('id' => $profile_id))->row_array();  
            $data['followers'] = $this->db->get_where('followers', array('following_id' => $profile_id))->num_rows();       
            $data['following'] = $this->db->get_where('followers', array('follower_id' => $profile_id))->num_rows();
            $data['doesFollow'] = $this->db->get_where('followers', array('follower_id' => $curr_user, 'following_id' => $profile_id))->num_rows();
            $data['posts'] = $this->db->get_where('posts', array('user_id' => $profile_id))->result_array();        

            $data['main_content'] = 'profile';  
            $this->load->view('template', $data);   

            $this->get_profile_view($profile_id, $ip, $curr_user);  
        }
    }

    protected function displayPageNotFound() {
        $this->output->set_status_header('404');
        $this->load->view('page_not_found');
    }
}

The only thing left to implement is the muser model with the checkIfUsername() method. Let me know if you need more information on this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top