2015-06-25 30 views
2

CodeIgniter URI路由規則中的「(:any)」和「:any」之間有什麼區別?例如:CodeIgniter中「(:any)」和「:any」之間的區別是什麼?

segment_1/segment_2/:any = my_controller/function/$1 

而且

segment_1/segment_2/(:any) = my_controller/function/$1 

我沒有看到在CI文檔的解釋,不知道。 :)

+0

我不認爲有任何區別。只需看一下源代碼,它只檢查':any'或':num'(不包括括號) – Craig

+1

我會堅持使用括號'(:any)',因爲這是文檔所說的:[** Codeigniter路由通配符**](http://www.codeigniter.com/user_guide/general/routing.html?highlight=route#wildcards) – CodeGodie

回答

1

有任何和(:任何)之間的區別。

首先(:any)替換爲$1秒(:any) 2 $等

:any更換沒有任何效果。

舉個例子,假設你有一個函數名myfunction的測試控制器需要一個arguemnt $一個這樣

class Test extends CI_Controller 
{ 
    public function myfunction($a='') 
    { 

     echo $a; 
    } 
} 

打這個網址baseurl/test/asdf

$route['test/(:any)']='test/myfunction/$1'; 
//$1== asdf 
//outputs asdf 

$route['test/:any']='test/myfunction/$1'; 
//$1!=asdf 
//outputs $1 

希望大家理解上的差異。

相關問題