Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing multiple request params with the same name in Symfony2

I'm posting the following values to a Symfony2 web page:

code=-1&tracking=SRG12891283&description=Error&code=0&tracking=SRG19991283&description=Label Printed.

Note the duplicates - there could be any number of code/tracking/description 'pairs'.

In Symfony, when I do the following, it only outputs the last set of values:

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

i.e.

code = 0 tracking = SRG19991283 desription = Label Printed.

I'm assuming this is because the request class stores the parameters in key/value pairs and so subsequent params are simply overwriting the previous ones.

Any idea how I can access all of these values?

like image 720
beterthanlife Avatar asked Feb 12 '26 20:02

beterthanlife


1 Answers

If you use "array-like" syntax in your parameters, Symfony should do what you want.

For example, consider a querystring of ?code[0]=a&code[1]=b&code[2]=c.

$request->query->get('code'); in Symfony would return an array like this: [ 0 => "a", 1 => "b", 2 => "c", ]

... which I think is what you want? (Albeit this is a simpler example.)

like image 184
Sam Avatar answered Feb 15 '26 13:02

Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!