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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2991323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:20:41  来源:igfitidea点击:

How to hide controller name in the url in CodeIgniter?

phpcodeigniter

提问by qmmr

so the thing is I'm using .htaccess to hide the index.php but I still get the controller name in the url like that: http://example.com/name_controller/aboutMy question is: is it possible to hide the name of the controller, so that only method is shown? hxxp://example.com/name_controller/about

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

回答by Justin Ethier

You can define a custom route in config/routes.php - for example:

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

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

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

See Hiding the controller's method name in the URL?in the CI forums for more information.

请参阅在 URL 中隐藏控制器的方法名称?在 CI 论坛中获取更多信息。

回答by jeroen

You can add an entry in the /system/application/config/routes.phpfile:

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

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

回答by pawan sen

I did it like this: (config/routes.php)

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

Code: $route['((photos|blogs).+)'] = "$1";

代码:$route['((photos|blogs).+)'] = "$1";

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

$route['([a-zA-Z0-9_-]+)'] = "用户/个人资料/$1";

it is ok correct solutions for common.

对于 common 来说,正确的解决方案是可以的。

回答by Hussy Borad

$route['default_controller'] = "xxx";

$route['default_controller'] = "xxx";

home

$route['home'] = "xxx/home";

$route[' home'] = " xxx/home";

function_name/parameter0/parameter1/parameter2

函数名/参数0/参数1/参数2

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

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

回答by Dhiraj Maurya

You can add below code in the /application/config/routes.phpfile:

您可以在/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/";//search product list perameters.

100% It work..

100% 有效..