[英]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.