我正在尝试创建自定义配置文件网址,例如site / u / username 我找到了这个How to make url /username in codeigniter?
但直到现在它仍然显示404.当我尝试访问网站/ u /用户名
时my routes.php
$route['default_controller'] = "guest";
$route['404_override'] = 'u';
我的模特
function cekuser($username)
{
$query = $this->db->query("SELECT * FROM usertable WHERE username = '$username'");
$query = $query->result_array();
if($query){
return $query[0];
};
}
我的控制器
<?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');
}
}
答案 0 :(得分:3)
而不是$route['404_override'] = 'u';
添加
$route['u/(:any)'] = 'u/showuserinfo';
添加方法
function showuserinfo($name)
{
$this->load->model('m_login');
if (empty($name))
{
$name = $this->uri->segment(2);
}
$user = $this->m_login->cekuser($name);
if (!empty($user))
{
$this->load->view('template', $user);
}
else
{
echo 'User Not found';
}
}
答案 1 :(得分:1)
$route['404_override'] = 'controller name';
添加
$route['controller name/(:any)'] = 'controller name/any name';