From a493fae4733e10407b398e98e7a279f3f1775be7 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 15 Apr 2024 20:40:36 -0400 Subject: [PATCH 1/4] Add ability to generate a URL with query parameters --- src/Illuminate/Routing/UrlGenerator.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 74c40d87cbbd..f15b086ee16f 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -230,6 +230,28 @@ public function to($path, $extra = [], $secure = null) ).$query; } + /** + * Generate an absolute URL with query parameters to the given path. + * + * @param string $path + * @param array $params + * @param mixed $extra + * @param bool|null $secure + * @return string + */ + public function query($path, $params = [], $extra = [], $secure = null) + { + [$path, $query] = $this->extractQueryString($path); + + parse_str(Str::after($query, '?'), $pairs); + + $query = Arr::query( + array_merge($pairs, $params) + ); + + return $this->to($path . '?' . $query, $extra, $secure); + } + /** * Generate a secure, absolute URL to the given path. * From 7c40392dbfbec5964bd395aec2f04618fe8557d1 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 15 Apr 2024 20:41:05 -0400 Subject: [PATCH 2/4] Add tests --- tests/Routing/RoutingUrlGeneratorTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 6fdd50f2ce2a..a19edaeaad19 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -42,6 +42,21 @@ public function testBasicGeneration() $this->assertSame('https://www.foo.com/foo/bar', $url->to('foo/bar')); } + public function testQueryGeneration() + { + $url = new UrlGenerator( + new RouteCollection, + 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?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('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']]))); + } + public function testAssetGeneration() { $url = new UrlGenerator( From 081c6712db0ad9eed93452ab4848f37c262eb680 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 15 Apr 2024 21:00:02 -0400 Subject: [PATCH 3/4] CS fixes --- src/Illuminate/Routing/UrlGenerator.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index f15b086ee16f..be6b783c729a 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -233,10 +233,10 @@ public function to($path, $extra = [], $secure = null) /** * Generate an absolute URL with query parameters to the given path. * - * @param string $path - * @param array $params - * @param mixed $extra - * @param bool|null $secure + * @param string $path + * @param array $params + * @param mixed $extra + * @param bool|null $secure * @return string */ public function query($path, $params = [], $extra = [], $secure = null) @@ -249,7 +249,7 @@ public function query($path, $params = [], $extra = [], $secure = null) array_merge($pairs, $params) ); - return $this->to($path . '?' . $query, $extra, $secure); + return $this->to($path.'?'.$query, $extra, $secure); } /** From 648d9d2a80b7323cd3a911fda067cd1d9758f78e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 16 Apr 2024 12:56:36 -0500 Subject: [PATCH 4/4] formatting --- src/Illuminate/Routing/UrlGenerator.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index be6b783c729a..10f18af5ca48 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -231,25 +231,23 @@ public function to($path, $extra = [], $secure = null) } /** - * Generate an absolute URL with query parameters to the given path. + * Generate an absolute URL with the given query parameters. * * @param string $path - * @param array $params + * @param array $query * @param mixed $extra * @param bool|null $secure * @return string */ - public function query($path, $params = [], $extra = [], $secure = null) + public function query($path, $query = [], $extra = [], $secure = null) { - [$path, $query] = $this->extractQueryString($path); - - parse_str(Str::after($query, '?'), $pairs); + [$path, $existingQueryString] = $this->extractQueryString($path); - $query = Arr::query( - array_merge($pairs, $params) - ); + parse_str(Str::after($existingQueryString, '?'), $existingQueryArray); - return $this->to($path.'?'.$query, $extra, $secure); + return $this->to($path.'?'.Arr::query( + array_merge($existingQueryArray, $query) + ), $extra, $secure); } /**