Skip to content

Commit

Permalink
Trim trailing ? from generated URL without query params (#51191)
Browse files Browse the repository at this point in the history
  • Loading branch information
onlime authored Apr 24, 2024
1 parent 410e9df commit f8300e3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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), '?');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']])));
}
Expand Down

0 comments on commit f8300e3

Please sign in to comment.