Skip to content

Change recommended way to access array query string parameters #16865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2022

Conversation

jontjs
Copy link
Contributor

@jontjs jontjs commented Jun 10, 2022

$request->query->all()['foo']; triggers an 'Undefined array key "foo"' error when foo doesn't exist. However $request->query->all('foo'); return null when foo doesn't exist (consistent with $request->query->get('bar'); for accessing a string parameter bar which might not exist).

Copy link
Contributor

@mdoutreluingne mdoutreluingne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return null when foo doesn't exist.

Return empty array to be more precise.

I approuve this PR, the change of the signature of this method all(string $key = null) which was introduced in Symfony 6.0, now lets put a key as a parameter to this method, which makes it possible to avoid an error if the key does not exist.

@jontjs
Copy link
Contributor Author

jontjs commented Jun 12, 2022

return null when foo doesn't exist.

Return empty array to be more precise.

Thank you for that pointing that out. Given that it's a different to what ->get('bar') returns if bar does not exist (that returns null), I thought it would be a good idea to update my PR to explicitly state that an empty array is returned.

@javiereguiluz javiereguiluz added this to the 6.0 milestone Jun 16, 2022
jontjs and others added 2 commits June 16, 2022 17:30
`$request->query->all()['foo'];` triggers an 'Undefined array key "foo"' error when `foo` doesn't exist. However `$request->query->all('foo');` return null  when `foo` doesn't exist (consistent with `$request->query->get('bar');` for accessing a string parameter `bar` which might not exist).
@javiereguiluz
Copy link
Member

Thank you Jon.

@javiereguiluz javiereguiluz merged commit 4f47e8d into symfony:6.0 Jun 16, 2022
@jontjs jontjs deleted the patch-3 branch May 19, 2023 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants