2017-08-08 86 views
0

我從Codeigniter 2遷移到3.我有默認控制器目錄中的控制器文件以及名爲內容和索引的子目錄。當前routes.php文件看起來像這樣:Codeigniter路由:任何

$controller_dir = opendir(APPPATH."controllers"); 
while (($file = readdir($controller_dir)) !== false) { 
    if (substr($file, -4) == ".php") { 
     $route[substr($file, 0, -4)."(.*)"] = substr($file, 0, -4)."$1"; 
    } else if (substr($file, -5) == ".php/") { 
     $route[substr($file, 0, -5)."(.*)"] = substr($file, 0, -5)."$1"; 
    } 
} 

$route['reviews'] = 'content/reviews/index/profile/$1'; 
$route['money/(:any)'] = 'indexes/money/index/$1'; 
$route['faq/do-i-need-to-signup'] = 'faq/question_answer'; 
$route['(:any)'] = 'index/profile/$1'; 

index/profile/$1具有邏輯數據庫來檢查用戶名。我將它用作www.example.com/robert,其中robert是動態名稱(將其視爲referal名稱)。它在CI 2中工作。但是,在CI 3獲得相同代碼的404錯誤。

我在這裏錯過了什麼?請問我問題不清楚。

+3

你不應該有一個名爲「索引」控制器。 https://codeigniter.com/userguide3/general/reserved_names.html#controller-names – Narf

+0

什麼文件會以'.php /'結尾? – Tpojka

回答

1

現在您需要在路徑中指定它將在查詢部分中使用更多參數。試試這個:

$route['reviews/(:any)'] = 'content/reviews/index/profile/$1'; 

這應該解決404錯誤。您將能夠訪問它:http://localhost/reviews/robert

如果您想傳遞更多參數,只需在路由中指定,然後將其設爲$number

例如爲:

$route['reviews/(:any)/(:any)'] = 'content/reviews/index/profile/$1/$2'; 
+0

感謝您的回答。我現在已經這樣解決了。但是,我想知道是否可以在URL中沒有「評論」字。 –

+0

你可以寫任何關鍵詞,比如'$ route ['anyword /(:any)/(:any)'] ='content/reviews/index/profile/$ 1/$ 2'' 但是它真的推薦使用'anyword '否則它不會查找其他控制器並嘗試映射此功能。 –