Contents

CodeIgniter3 設定 Router

Contents

routes.php可以設定CodeIgniter路由
這邊是設定所有路由
好像跟Rails設定方法有點不太一樣
不過也滿好懂的

1
2
3
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

預設導置Controller的Welcome.php那隻(PS:Contoller規定一開始要大寫)
‘welcome’ 後面method沒寫,就是導置welcome/index

當上面條件抓到,就會馬上導到Contoller
所以上下規則還算滿重要的

官網這篇寫的還滿清楚的
https://codeigniter.org.tw/userguide3/general/routing.html

(:num) 將匹配只含有數字的一個片段。 (:any) 將匹配含有任何字元的一個片段(除了 ‘/’,這是區段界定符號)。
萬用字元實際上是正規表達式的別名, :any 被翻譯成 [^/]+ 以及 :num 被翻譯成 [0-9]+ 。

1
2
$route['login/(.+)'] = 'auth/login/$1';
$route['login/(:any)'] = 'auth/login/$1';

簡單可以用正規化來定義路由設定

#路由中使用 HTTP 動詞

例如:
$route[‘products’][‘put’] = ‘product/insert’;
上述例子,PUT 請求到 URI“products” 稱之為 Product::insert() 控制器方法。
$route[‘products/(:num)’][‘DELETE’] = ‘product/delete/$1’;
DELETE 請求到 URL“products”第一個片段,數字在第二個片段將會重新映射到 Product::delete() 方法,傳入數值到第一個參數上。
使用 HTTP 動詞當然是可選的(非必要)。

還有此功能,有空來試試吧:P