$route['default_controller'] = 'home';
$route['404_override'] = 'home';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = "home/$1";
$route['(:any)/(:any)'] = "home/$1/$1";
$route['(:any)/(:any)/(:any)'] = "home/$1/$1/$1";
$route['(:any)/(:any)/(:any)/(:any)'] = "home/$1/$1/$1/$1";
$route['(:any)/(:any)/(:any)/(:any)/(:any)'] = "home/$1/$1/$1/$1/$1";
$route['project'] = "project";
$route['project/(:any)'] = 'project/$1';
我有默认的家庭控制器,我添加了额外的$ 1参数正在工作。 家庭控制器完全正常工作,但我想要项目控制器
$route['project'] = "project"; Is working
但
$route['project/(:any)'] = 'project/$1';
它不起作用我不知道为什么请帮我提前
答案 0 :(得分:1)
希望这会对您有所帮助:
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['project'] = "project";
$route['project/(:any)'] = 'project/$1';
$route['project/(:any)/(:any)'] = 'project/$1/$2';
/*this should be after the project route setting or in the last
according to the precedence of url it always call the first match */
$route['(:any)'] = "home/$1";
$route['(:any)/(:any)'] = "home/$1/$1";
$route['(:any)/(:any)/(:any)'] = "home/$1/$1/$1";
$route['(:any)/(:any)/(:any)/(:any)'] = "home/$1/$1/$1/$1";
$route['(:any)/(:any)/(:any)/(:any)/(:any)'] = "home/$1/$1/$1/$1/$1";
建议你home
控制器的路线应该是这样的:
/*home should start with name home where first `(:any)` tends to method name and
second `(:any)` tends to parameters*/
$route['home'] = "home";
$route['home/(:any)'] = "home/$1";
$route['home/(:any)/(:any)'] = "home/$1/$2";
/*you can add many more like this*/
答案 1 :(得分:0)
你从不在路线中提到方法名称,这就是路线不起作用的原因
试试这个
$route['project/(:any)'] = 'project/method_name/$1'
同样home
路由应该是home/method_name/$1
,home/index/$1/$1
等
答案 2 :(得分:0)
您正在致电
$route['project'] = "project";
它将运行您的project
控制器index
功能,因此可以正常运行
$route['project/(:any)'] = 'project/method_name/$1'
如果您这样打电话会搜索project
控制器method_name
功能,那么您应该通过parameter
。因为您在路线中添加了(:any)
如果在路线中使用(:any)
或(:num)
,则应捕捉函数中的参数。
例如
$route['project/(:any)'] = 'project/method_name/$1'
function method_name($param1){
//your code
}
$route['project/(:any)/(:any)'] = 'project/method_name/$1/$2'
function method_name($param1, $param2){
//your code
}
您可以在路线中使用(.*)
来获取无限参数。而且您不需要捕获函数中的参数。检查下面。
$route['default_controller'] = 'home';
$route['404_override'] = 'home';
$route['translate_uri_dashes'] = FALSE;
$route['(.*)'] = "home";
$route['project'] = "project";
$route['project/(.*)'] = 'project';
使用$this->uri->segment()
捕获函数
class Home extends CI_Controller {
function index(){
$param1 = $this->uri->segment(1);
$param2 = $this->uri->segment(2);
$param3 = $this->uri->segment(3);
}
}
class Project extends CI_Controller {
function index(){
$param1 = $this->uri->segment(1);
$param2 = $this->uri->segment(2);
$param3 = $this->uri->segment(3);
}
}