2013-02-18 63 views
0

我在尋找的建議如何避免:由具有更寬容的單一模式,如果可能的話,在此路由不會在所有破我試圖避免笨路線的正則表達式矯枉過正

$route = array(
    'directory'    => 'accountdirectory/index', 
    'directory/(:any)'   => 'accountdirectory/index/$1', 
    'directory/(:any)/(:num)'  => 'accountdirectory/index/$1/$2' ... 

段中不存在:

'directory(/)?(:any)?(/)?(:any)' => 'accountdirectory/index/$1/$2'

爲了路線

  • 目錄/所有
  • 目錄/
  • 目錄/所有/ 10
+1

你就不能路線accountdirectory /索引,然後使用URI段和如果從那裏重定向? – Rooster 2013-02-18 20:51:08

+0

你說得對。這似乎是我嘗試做的最好的做法。如果你不想離開這個答案,我肯定會接受它的。 – Rell 2013-02-20 05:12:27

回答

1

您可以可以使用一個路線:

$route = array(
    'directory' => 'accountdirectory/index', 
    ... 
) 

並定義accountdirectory /指數函數是這樣的:

public function index($arg1 = null, $arg2 = null) { 
    if ($arg === 'all' && $arg2 !== null) { 
     // directory/all/XXXX 
    } 
    elseif ($arg1 === 'all') { 
     // directory/all 
    } 
    else { 
     ... 
    } 
} 
0

一般我會在情況下,像你描述做的是:

$route['404_override'] = 'directory'; 

,那麼你可以在使用if或switch語句構造函數或索引函數根據其他uri段從那裏重定向。