2012-11-15 41 views
1

我張貼下列值到Symfony2的網頁訪問多個請求參數名稱相同:在Symfony2中

碼= -1 &跟蹤= SRG12891283 &描述=錯誤&碼= 0 &跟蹤= SRG19991283 & description =標籤打印。

請注意重複 - 可能有任何數量的代碼/跟蹤/說明'對'。

在symfony中,當我執行以下操作,它只輸出最後的一組值:

foreach($request->request->all() as $key => $val){ 
    $this->m_logger->debug($key . ' - ' .$val); 
} 

代碼= 0 跟蹤= SRG19991283 desription =標籤印刷。

我假設這是因爲請求類將參數存儲在鍵/值對中,因此後續參數只是覆蓋以前的參數。

任何想法如何訪問所有這些值?

回答

1

$ _REQUEST,$ _POST和$ _GET數組中的PHP將使用變量的最後一個定義覆蓋重複的變量名。 Symfony2因此表現出相同的行爲。

例如給出的代碼。

<?php 
echo "<pre>"; 
var_dump($_GET); 
var_dump($_POST); 
var_dump($_REQUEST); 
echo "</pre>"; 
?> 
<form method="post"> 

<input type="text" name="test1" value="1"/> 
<input type="text" name="test2" value="2"/> 
<input type="text" name="test2" value="3"/> 
<input type="submit"/> 
</form> 

提交表格後,輸出的是

array(0) { 
} 
array(2) { 
    ["test1"]=> 
    string(1) "1" 
    ["test2"]=> 
    string(1) "3" 
} 
array(2) { 
    ["test1"]=> 
    string(1) "1" 
    ["test2"]=> 
    string(1) "3" 
} 

查詢字符串調用頁面?test1=1&test2=2&test2=3結果是:

array(2) { 
    ["test1"]=> 
    string(1) "1" 
    ["test2"]=> 
    string(1) "3" 
} 
array(0) { 
} 
array(2) { 
    ["test1"]=> 
    string(1) "1" 
    ["test2"]=> 
    string(1) "3" 
} 

解決這個問題自己會的唯一途徑將查詢字符串(GET)傳遞給變量,在這種情況下,您可以檢索查詢字符串並自行解析。如果您處理用戶輸入,這可能不合適。

0

如果在參數中使用「類似數組」的語法,Symfony應該做你想做的。

例如,考慮查詢字符串?code[0]=a&code[1]=b&code[2]=c

$request->query->get('code');中的Symfony會返回一個這樣的數組: [ 0 => "a", 1 => "b", 2 => "c", ]

...我認爲這是你想要的嗎? (儘管這是一個更簡單的例子。)

+0

如果你在Symfony中也建立了發佈端,你可以設置一個路由參數,代碼=> ['a','b','c' ]'來獲得查詢字符串。 – Sam