如何在codeigniter中创建url/username?

lea*_*arn 2 php uri friendly-url codeigniter username

我现在正在使用codeigniter.我目前能够通过使用来查看个人资料,/user/profile/profile_id但我希望能够让用户只需导航到个人资料/username以使其更简单.

我怎么会这样做我不知道从哪里开始?

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);  

}

} 
Run Code Online (Sandbox Code Playgroud)

routes.php文件

$route['default_controller'] = "signin";
$route['404_override'] = '';
Run Code Online (Sandbox Code Playgroud)

小智 6

我认为您尝试实现的功能如下:

  1. 如果用户导航到http://example.com/(路由)并且路由由控制器映射,则显示该控制器.
  2. 如果用户导航到http://example.com/(路由)并且路由未由任何控制器映射,则路由有可能是用户名,因此:
    1. 如果路线是用户名,则显示该人的路线.
    2. 如果路由不是用户名,则显示404页面.

所以这里的计划是创建一个自定义404处理程序,检查提供的路由是否是用户名,否则,它显示404页面.

我们需要做的第一件事是设置我们的自定义404控制器:

$route['404_override'] = 'profile';
Run Code Online (Sandbox Code Playgroud)

然后我们创建自定义404控制器:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

唯一要实现的是具有checkIfUsername()方法的muser模型.如果您需要更多相关信息,请与我们联系.