2012-05-05 24 views
1

對於我的Rest WebService,我需要一些變體來放置城市或區域或其他內容。所以我的URI應該是這樣的:帶問號的URI段

/rest/rating/locations?city=London 

現在,我使用下面的函數來獲得最後的URI段:

$segcount = $this->uri->total_segments(); 
$lastseg = $this->uri->segment($segcount); 

的問題是在這裏從問號寄託都被板缺! 它會保存變量就是:

我試過配置位置以下的config.php文件:

$config['uri_protocol'] = 'PATH_INFO'; 
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?'; 
$config['enable_query_strings'] = TRUE; 

是否有任何其他的可能性,以節省問號整段?

+0

我很困惑,'$ this-> uri-> total_segments()'只返回**#**段。你不想要這個:'$ this-> uri-> segment_array()' –

+1

@tim:他的方法獲取* last * URL段(無論什麼原因)。 –

+0

是的,我需要最後一段,之後我將使用'$ city = substr($ lastseg,strlen(「locations?city =」))'來獲得城市的字符串 – milepile

回答

0

在您的配置文件,確保設置爲TRUE如下:

$config['allow_get_array']= TRUE; 
+1

這是艾爾迪集。 – milepile

+0

看到上面的新評論,我也加入到我的答案了...... –

+0

@milepile:仔細閱讀,你沒有說你設置了這個 - 你說你使用了'enable_query_strings'。兩件完全不同的事情。我知道這很混亂,我希望他們會放棄對該功能的支持。看到這個頁面的底部(並仔細閱讀):http://codeigniter.com/user_guide/general/urls.html –

2

首先,請確保您有:

$config['allow_get_array'] = TRUE; 

enable_query_strings其實完全是另一回事,老的特性是沒用太多。

URI類仍然無法幫到你,你必須單獨引用查詢字符串。

$segcount = $this->uri->total_segments(); 
$lastseg = $this->uri->segment($segcount); 

// Either of these should work 
$query_string = http_build_query($this->input->get()); 
$query_string = $this->input->server('QUERY_STRING'); 

$lastseg_with_query = $lastseg.'?'.$query_string;