php - 如何在 codeigniter 中创建 url/用户名?

标签 php codeigniter uri friendly-url

我目前正在使用 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);  

}

} 

路由.php

$route['default_controller'] = "signin";
$route['404_override'] = '';

最佳答案

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

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

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

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

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

然后我们创建自定义 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');
    }
}

唯一需要实现的是带有 checkIfUsername() 方法的 muser 模型。如果您需要这方面的更多信息,请告诉我。

关于php - 如何在 codeigniter 中创建 url/用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19336567/

相关文章:

php - 使用数据库中的值和列名动态创建输入字段

php - 投票脚本,简化数据库查询的可能性

php - 如何使用 PHP 发布表单序列化数据?

mysql - Tank auth(CodeIgniter 库)中的 users 和 user_profiles 表有什么区别?

c# - 是否有存储 UNC 路径的数据类型?

c# - Uri.EscapeUriString - 如何使用它?

php - 从 ajax 加载的脚本访问 JavaScript 函数

PHP 数据库中没有任何内容

php - 无法使用 codeigniter 从数据库中检索数据

android - URI 和位图图像之间的区别