我目前正在使用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);
}
}
routes.php
$route['default_controller'] = "signin";
$route['404_override'] = '';
我认为您要实现的功能如下:
因此,这里的计划是创建一个自定义的404处理程序,以检查提供的路由是否为用户名,否则将显示404页面。
我们需要做的第一件事是设置自定义404控制器:
$route['404_override'] = 'profile';
然后,我们创建我们的自定义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');
}
}
剩下要实现的就是带有checkIfUsername()方法的muser模型。让我知道您是否需要更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句