PHP Codeigniter中的URL路由

问题描述:

我正在route.php中输入- $ route ['admin / students'] =‘view_student'。这里的view_student是控制器名称。现在,当从 localhost / school / admin页面调用< a href = admin / students> Students< / a> 时,一切正常。但是,当我更改路线时,例如- $ route ['/ school / admin / students'] ='view_student',然后从 localhost / school / admin页面调用它如< a href = / school / admin / students> Students< / a> 所示,则显示404页。

I am having entry in my route.php like - $route['admin/students'] = 'view_student'. Here view_student is controller name. Now when from "localhost/school/admin" page I call <a href="admin/students">Students</a>, than everything works fine; But when I change my route like - $route['/school/admin/students'] = 'view_student', and call it from "localhost/school/admin" page as <a href="/school/admin/students">Students</a>, than 404 page is shown. Whats wrong in here?

尝试使用此代码可能会对您有所帮助:

此处仪表板是控制器的名称

Here dashboard is the name of controller

//this will route as localhost/appFolder/admin/index
  $route['admin'] = 'dashboard'; // for your index page

//this will route as localhost/appFolder/admin/method_name
 $route['admin/(:any)'] = 'dashboard/$1';

//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';

链接路线类似

// for your index page
<a href="<?php echo base_url('admin/index'); ?>"></a>

// for your other pages
<a href="<?php echo base_url('admin/method_name'); ?>"></a>

要链接定义的其他控制器,就像

 <a href="<?php echo base_url('otherControllerName/method_name'); ?>"></a>