[英]How to remove controller name from url making it clean in codeigniter
I have the following url..我有以下网址..
http://localhost/ci/site_controller/home
I want to remove site_controller
controller from url resulting in..我想从 url 中删除site_controller
控制器导致..
http://localhost/ci/home
How can I do this in CodeIgniter?我如何在 CodeIgniter 中执行此操作?
Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.注意:如果你问我试过什么,而不是我刚刚在谷歌上搜索过,因为我不知道如何使用 mod_rewrite。
EDIT编辑
I have this in my routes.php我在我的 routes.php 中有这个
$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';
but still not working!但还是不行!
.htaccess .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
You can set your a route for each url:您可以为每个网址设置路线:
In your config/routes.php file, just set each page like this:在您的 config/routes.php 文件中,只需像这样设置每个页面:
$route['ci/home'] = "ci/site_controller/home";
This might help you to define a Default Controller for your CodeIgniter project.这可能会帮助您为您的 CodeIgniter 项目定义默认控制器。
https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller
For instance, in your case, open your application/config/routes.php file and set this variable:例如,在您的情况下,打开您的 application/config/routes.php 文件并设置此变量:
$route['default_controller'] = 'site_controller';
assuming you currently have your url http://localhost/ci/site_controller/home
you can just write an explicit route like below to your /application/config/routes.php
假设你目前有你的 url http://localhost/ci/site_controller/home
你可以写一个像下面这样的显式路由到你的/application/config/routes.php
$route['ci/home'] = 'site_controller/home';
or要么
$route['ci/home'] = 'ci/site_controller/home';
if your controller is namespaced to /application/controllers/ci/
如果您的控制器命名空间为/application/controllers/ci/
This helped for me.这对我有帮助。 In " routes.php
" I added this:在“ routes.php
”中,我添加了这个:
$route['(:any)'] = "default_controller/$1"; $route['(:any)'] = "default_controller/$1";
try using the routes file to re-map url's尝试使用路由文件重新映射 url
http://ellislab.com/codeigniter/user_guide/general/routing.html http://ellislab.com/codeigniter/user_guide/general/routing.html
I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/我刚刚在此链接中找到了解决方案,它对我有用: http ://ellislab.com/forums/viewthread/148531/
$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";
Hope it helps you:)希望对你有帮助:)
In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...如果你的 .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]
And your codeigniter code lies directly in the html folder...而您的 codeigniter 代码直接位于 html 文件夹中...
To remove the controller name from URL, edit application/config/routes.php Add lines like these:要从 URL 中删除控制器名称,请编辑application/config/routes.php添加如下行:
$route['signin'] = 'web/signin';
The line above calls function signin() of controller Web when user requests yoursebsite.com/signin当用户请求 yoursebsite.com/signin 时,上面的行调用控制器Web的函数 signin ()
To pass variables into said function, do the following:要将变量传递给所述函数,请执行以下操作:
$route['p/(:any)'] = 'web/p/$1';
This line calls function p() in controller Web , which has a parameter.该行调用控制器Web中的函数p() ,它有一个参数。 Function p() was defined as:函数p()定义为:
public function p($param = "none") {
echo $param;
}
I hope this helps:)我希望这有帮助:)
It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x in my case URL was: http://localhost:8080/index.php/page/sometext Where "Page" is the controller name open application/config/routs.php如果您想从 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";
Result: http://localhost:8080/index.php/sometext结果:http://localhost:8080/index.php/sometext
I assure you, it will work.我向你保证,它会起作用的。 tested 100%. 100% 测试。
Drop the 'ci'
from your routes.从您的路线中删除'ci'
。 That is your project name and is never required.那是您的项目名称,永远不需要。 Routes always start on the controller level:路由总是从控制器级别开始:
$route['home'] = "site_controller/home";
Will work.将工作。 However, more importantly.. are you sure your design is correct?然而,更重要的是..你确定你的设计是正确的吗? Why not just let home
be a controller?为什么不让home
成为控制器呢? Create a /application/controllers/home.php
and you'll be able to access it's index()
function with http://localhost/ci/home
.创建一个/application/controllers/home.php
,您将能够使用http://localhost/ci/home
访问它的index()
函数。
You're complicating things.你把事情复杂化了。 No need for routes at all.根本不需要路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.