From f8300e38191664e4da40fa36af1d26b482fc4239 Mon Sep 17 00:00:00 2001 From: "Philip Iezzi (Pipo)" <2759561+onlime@users.noreply.github.com> Date: Wed, 24 Apr 2024 21:54:03 +0200 Subject: [PATCH] Trim trailing `?` from generated URL without query params (#51191) --- src/Illuminate/Routing/UrlGenerator.php | 4 ++-- tests/Routing/RoutingUrlGeneratorTest.php | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 10f18af5ca48..3bfd6127d464 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -245,9 +245,9 @@ public function query($path, $query = [], $extra = [], $secure = null) parse_str(Str::after($existingQueryString, '?'), $existingQueryArray); - return $this->to($path.'?'.Arr::query( + return rtrim($this->to($path.'?'.Arr::query( array_merge($existingQueryArray, $query) - ), $extra, $secure); + ), $extra, $secure), '?'); } /** diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index a19edaeaad19..1d92d5e6e1b0 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -49,10 +49,13 @@ public function testQueryGeneration() Request::create('http://www.foo.com/') ); - $this->assertSame('http://www.foo.com/foo/bar?', $url->query('foo/bar')); + $this->assertSame('http://www.foo.com/foo/bar', $url->query('foo/bar')); $this->assertSame('http://www.foo.com/foo/bar?0=foo', $url->query('foo/bar', ['foo'])); $this->assertSame('http://www.foo.com/foo/bar?baz=boom', $url->query('foo/bar', ['baz' => 'boom'])); $this->assertSame('http://www.foo.com/foo/bar?baz=zee&zal=bee', $url->query('foo/bar?baz=boom&zal=bee', ['baz' => 'zee'])); + $this->assertSame('http://www.foo.com/foo/bar?zal=bee', $url->query('foo/bar?baz=boom&zal=bee', ['baz' => null])); + $this->assertSame('http://www.foo.com/foo/bar?baz=boom', $url->query('foo/bar?baz=boom', ['nonexist' => null])); + $this->assertSame('http://www.foo.com/foo/bar', $url->query('foo/bar?baz=boom', ['baz' => null])); $this->assertSame('https://www.foo.com/foo/bar/baz?foo=bar&zal=bee', $url->query('foo/bar?foo=bar', ['zal' => 'bee'], ['baz'], true)); $this->assertSame('http://www.foo.com/foo/bar?baz[0]=boom&baz[1]=bam&baz[2]=bim', urldecode($url->query('foo/bar', ['baz' => ['boom', 'bam', 'bim']]))); }