From e8d6764e32b4be8161fe0d7f933504b4a67104f5 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 2 Oct 2015 15:56:23 +0200 Subject: [PATCH] [HttpFoundation] change precedence of parameters in Request::get --- .../Component/HttpFoundation/CHANGELOG.md | 5 +++++ .../Component/HttpFoundation/Request.php | 20 +++++++------------ .../HttpFoundation/Tests/RequestTest.php | 19 ++++++++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 9f48e8252d..ca98c04c6c 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index ee02340871..f3878f522f 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -700,19 +700,13 @@ class Request } /** - * 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 * @param mixed $default the default value @@ -721,11 +715,11 @@ class Request */ 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; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 3bdfc443d7..e190a8bd28 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -1221,6 +1221,25 @@ class RequestTest extends \PHPUnit_Framework_TestCase $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();