我现在在我的codeigniter配置中设置uri路由,就像这样
$route['user/:any'] = "users";
并且一切正常,请求像这样
http://localhost/user/1
但如果我尝试请求
http://localhost/user/
所以uri段是空的,我收到错误
404 Page Not Found
The page you requested was not found.
如何避免这种情况?我试过用
$user = $this->uri->segment(2);
if (!isset($user)) redirect('/', 'refresh');
但这不起作用。
答案 0 :(得分:4)
您可以添加其他路线,例如:
$route['user'] = "users"
答案 1 :(得分:3)
在您的控制器中构建您的功能而不是:
public function index()
{
...
尝试这样的事情:
public function index($slug = '')
{
if ($slug = '') {
// redirect code here
}
else {
// if there is something in $slug then load this code
}
也许你的路线应该是这样的:
$route['user'] = 'users';
$route['user/(:any)'] = 'users/index/$1';
答案 2 :(得分:1)
尝试
$route['user:any'] = "users";
答案 3 :(得分:1)
我认为您需要修复语法,请尝试:
$route['user/(:any)'] = "users";
您需要在通配符(:any)
周围加上括号。
参考:http://ellislab.com/codeigniter/user-guide/general/routing.html参见示例