繁体   English   中英

如何从 url 中删除控制器名称,使其在 codeigniter 中变得干净

[英]How to remove controller name from url making it clean in codeigniter

我有以下网址..

http://localhost/ci/site_controller/home

我想从 url 中删除site_controller控制器导致..

 http://localhost/ci/home

我如何在 CodeIgniter 中执行此操作?

注意:如果你问我试过什么,而不是我刚刚在谷歌上搜索过,因为我不知道如何使用 mod_rewrite。

编辑

我在我的 routes.php 中有这个

$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';

但还是不行!

.htaccess

RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

ErrorDocument 404 /index.php

您可以为每个网址设置路线:

在您的 config/routes.php 文件中,只需像这样设置每个页面:

$route['ci/home'] = "ci/site_controller/home";

这可能会帮助您为您的 CodeIgniter 项目定义默认控制器。

https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller

例如,在您的情况下,打开您的 application/config/routes.php 文件并设置此变量:

$route['default_controller'] = 'site_controller';

假设你目前有你的 url http://localhost/ci/site_controller/home你可以写一个像下面这样的显式路由到你的/application/config/routes.php

$route['ci/home'] = 'site_controller/home';

要么

$route['ci/home'] = 'ci/site_controller/home';

如果您的控制器命名空间为/application/controllers/ci/

这对我有帮助。 在“ routes.php ”中,我添加了这个:

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

尝试使用路由文件重新映射 url

http://ellislab.com/codeigniter/user_guide/general/routing.html

我刚刚在此链接中找到了解决方案,它对我有用: http ://ellislab.com/forums/viewthread/148531/

$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";

希望对你有帮助:)

如果你的 .htaccess 文件是这样设置的(或类似的),那么 index.php 已经被删除了......

RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

而您的 codeigniter 代码直接位于 html 文件夹中...

要从 URL 中删除控制器名称,请编辑application/config/routes.php添加如下行:

$route['signin'] = 'web/signin';

当用户请求 yoursebsite.com/signin 时,上面的行调用控制器Web的函数 signin ()

要将变量传递给所述函数,请执行以下操作:

$route['p/(:any)'] = 'web/p/$1';

该行调用控制器Web中的函数p() ,它有一个参数。 函数p()定义为:

public function p($param = "none") {
    echo $param;
}

我希望这有帮助:)

如果您想从 Codeigniter 3.x 中的 URL 中删除控制器名称非常简单,在我的例子中 URL 是: http://localhost:8080/index.php/page/sometext其中“Page”是控制器名称打开应用程序/配置/routs.php

$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";

结果:http://localhost:8080/index.php/sometext

我向你保证,它会起作用的。 100% 测试。

从您的路线中删除'ci' 那是您的项目名称,永远不需要。 路由总是从控制器级别开始:

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

将工作。 然而,更重要的是..你确定你的设计是正确的吗? 为什么不让home成为控制器呢? 创建一个/application/controllers/home.php ,您将能够使用http://localhost/ci/home访问它的index()函数。

你把事情复杂化了。 根本不需要路由。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM