我有Codeigniter项目我不知道写Route

时间:2018-05-14 03:27:37

标签: php codeigniter

$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'; 

它不起作用我不知道为什么请帮我提前

3 个答案:

答案 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/$1home/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);
    }
}