Custom Profile URL Codeigniter
我正在尝试创建自定义配置文件 URL,例如 site/u/username
我找到了这个 How to make url /username in codeigniter?
但直到现在它仍然显示 404。当我尝试访问站点/u/用户名时
我的路线.php
1 2 | $route['default_controller'] ="guest"; $route['404_override'] = 'u'; |
我的模型
1 2 3 4 5 6 7 8 | function cekuser($username) { $query = $this->db->query("SELECT * FROM usertable WHERE username = '$username'"); $query = $query->result_array(); if($query){ return $query[0]; }; } |
我的控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php class U extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { $username = $this->uri->segment(1); if (empty($username)) { $this->show_404(); } $this->load->model('m_login'); // Check if parameter is not a valid username. if (!$this->m_login->cekuser($username)) { $this->displayPageNotFound(); } else { $this->load->view('template'); } } protected function displayPageNotFound() { $this->output->set_status_header('404'); $this->load->view('notfound'); } } |
路由.php
而不是
添加
1 | $route['u/(:any)'] = 'u/showuserinfo'; |
在控制器中
添加方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
1 | $route['404_override'] = 'controller name'; |
添加
1 | $route['controller name/(:any)'] = 'controller name/any name'; |