3

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?

2 Answers 2

3

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

And if you were building the posting side in Symfony too, you'd set a route parameter of 'code' => ['a','b','c'] to get that querystring.
1

PHP in the $_REQUEST, $_POST, and $_GET arrays will overwrite a duplicated variable name with the last definition of the variable. Symfony2 as a result, exhibits the same behavior.

For example given the code.

<?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>

After submitting the form, the output is

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

Calling the page with the query string ?test1=1&test2=2&test2=3 the result is:

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

The only way to resolve this issue yourself would be to pass the variables as a query string (GET), in which case you could retrieve the query string and parse it yourself. This might not be appropriate if you're handling user input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.