如何在CodeIgniter的url中隐藏控制器名称?

时间:2010-06-07 16:34:01

标签: php codeigniter

所以我正在使用.htaccess隐藏index.php,但我仍然在网址中获取控制器名称:http://example.com/name_controller/about 我的问题是:是否可以隐藏控制器的名称,以便只显示方法? hxxp://example.com/name_controller/about

5 个答案:

答案 0 :(得分:14)

您可以在config / routes.php中定义自定义路由 - 例如:

$route['about'] = 'name_controller/about';

Then, http://example.com/about
goes to http://example.com/name_controller/about

有关详细信息,请参阅CI论坛中的Hiding the controller’s method name in the URL?

答案 1 :(得分:3)

您可以在/system/application/config/routes.php文件中添加条目:

$route['about'] = "controller_name/about";

答案 2 :(得分:2)

我是这样做的:(config / routes.php)

代码: $ route ['((照片|博客)。+)'] =“$ 1”;

$ route ['([a-zA-Z0-9 _-] +)'] =“user / profile / $ 1”;

这是正确的解决方案。

答案 3 :(得分:1)

$ route ['default_controller'] =“xxx”;

$ route [' home '] =“ xxx / home ”;

函数名/ parameter0 /参数1 /参数2

$ route [' Collection /(:any)'] =“ xxx / Collection / $ 1 ”;

答案 4 :(得分:-1)

您可以在 /application/config/routes.php 文件中添加以下代码:

$route['default_controller'] = "Home";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['(?i)about'] = "Home/about";
$route['(?i)login'] = "Home/Login";
$route['(?i)products'] = "ProductController/ProductList";
$route['(?i)addproduct'] = "ProductController/AddProduct";
$route['(?i)editproduct'] = "ProductController/EditProduct";
$route['(?i)products/(:any)'] = "ProductController/ProductList/$1";//search product list perameters.
  

100%有效。.