Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] change precedence of parameters in Request::get #16076

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 1 commit into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.0.0
-----

* The precedence of parameters returned from `Request::get()` changed from "GET, PATH, BODY" to "PATH, GET, BODY"

2.8.0
-----

Expand Down
20 changes: 7 additions & 13 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,19 +700,13 @@ public static function getHttpMethodParameterOverride()
}

/**
* Gets a "parameter" value.
* Gets a "parameter" value from any bag.
*
* This method is mainly useful for libraries that want to provide some flexibility.
* This method is mainly useful for libraries that want to provide some flexibility. If you don't need the
* flexibility in controllers, it is better to explicitly get request parameters from the appropriate
* public property instead (attributes, query, request).
*
* Order of precedence: GET, PATH, POST
*
* Avoid using this method in controllers:
*
* * slow
* * prefer to get from a "named" source
*
* It is better to explicitly get request parameters from the appropriate
* public property instead (query, attributes, request).
* Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY
*
* @param string $key the key
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is not relevant anymore with the removal of deep and also since the refactoring in #12369

* @param mixed $default the default value
Expand All @@ -721,11 +715,11 @@ public static function getHttpMethodParameterOverride()
*/
public function get($key, $default = null)
{
if ($this !== $result = $this->query->get($key, $this)) {
if ($this !== $result = $this->attributes->get($key, $this)) {
return $result;
}

if ($this !== $result = $this->attributes->get($key, $this)) {
if ($this !== $result = $this->query->get($key, $this)) {
return $result;
}

Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,25 @@ public function testGetPathInfo()
$this->assertEquals('/path%20test/info', $request->getPathInfo());
}

public function testGetParameterPrecedence()
{
$request = new Request();
$request->attributes->set('foo', 'attr');
$request->query->set('foo', 'query');
$request->request->set('foo', 'body');

$this->assertSame('attr', $request->get('foo'));

$request->attributes->remove('foo');
$this->assertSame('query', $request->get('foo'));

$request->query->remove('foo');
$this->assertSame('body', $request->get('foo'));

$request->request->remove('foo');
$this->assertNull($request->get('foo'));
}

public function testGetPreferredLanguage()
{
$request = new Request();
Expand Down