如何在codeigniter中使用url /用户名?
我现在使用codeigniter。我目前可以通过使用 / user / profile / profile_id
查看个人资料,但我想让用户可以导航到个人资料使用
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
routes.php
$route['default_controller'] = "signin";
$route['404_override'] = '';
我认为实现如下:
- 如果用户导航到 http://example.com/(route),路由由控制器映射,然后显示该控制器。
- 如果用户导航到 http://example.com/(route),且路由未由任何控制器映射,则路由是一个用户名的机会,因此:
- If the user navigates to http://example.com/(route) and route is mapped by a controller, then display that controller.
- 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:
- 如果路由是用户名,则显示该用户的路由。
- 如果路由不是用户名,请显示404页面。
因此,这里的计划是创建一个自定义404处理程序,
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.
我们需要做的第一件事是设置我们的自定义404控制器:
The first thing we need to do is to set our custom 404 controller:
$route['404_override'] = 'profile';
然后创建我们的自定义404控制器:
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');
}
}
剩下唯一要实现的是muser模型checkIfUsername()方法。如果您需要更多相关资讯,请与我们联络。
The only thing left to implement is the muser model with the checkIfUsername() method. Let me know if you need more information on this.