From 325e9ac4bc1d8e97302a990360558857a8bb9f75 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 29 Apr 2018 17:50:30 +0200 Subject: [PATCH 1/2] Fix relative route URL generation with custom host formatter --- src/Illuminate/Routing/RouteUrlGenerator.php | 2 +- tests/Routing/RoutingUrlGeneratorTest.php | 23 ++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index b5a4c1cbf1ec..eec067e1be52 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -96,7 +96,7 @@ public function to($route, $parameters = [], $absolute = false) $uri = strtr(rawurlencode($uri), $this->dontEncode); if (! $absolute) { - return '/'.ltrim(str_replace($root, '', $uri), '/'); + return '/'.ltrim(preg_replace('#^(//|[^/?])+#', '', $uri), '/'); } return $uri; diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 766cade3a69b..587ec1c2b8a4 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -46,16 +46,31 @@ public function testBasicGeneration() $this->assertEquals('https://www.foo.com/foo/bar', $url->asset('foo/bar', true)); } - public function testBasicGenerationWithFormatting() + public function testBasicGenerationWithHostFormatting() + { + $url = new UrlGenerator( + $routes = new RouteCollection, + $request = Request::create('http://www.foo.com/') + ); + + $route = new Route(['GET'], '/named-route', ['as' => 'plain']); + $routes->add($route); + + $url->formatHostUsing(function ($host) { + return str_replace('foo.com', 'foo.org', $host); + }); + + $this->assertEquals('http://www.foo.org/foo/bar', $url->to('foo/bar')); + $this->assertEquals('/named-route', $url->route('plain', [], false)); + } + + public function testBasicGenerationWithPathFormatting() { $url = new UrlGenerator( $routes = new RouteCollection, $request = Request::create('http://www.foo.com/') ); - /* - * Empty Named Route - */ $route = new Route(['GET'], '/named-route', ['as' => 'plain']); $routes->add($route); From fc59103108da1a9fdcad688d1cac3e5b1d913ed5 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 29 Apr 2018 18:56:05 +0200 Subject: [PATCH 2/2] Remove unused variable --- src/Illuminate/Routing/RouteUrlGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index eec067e1be52..cc849bb8e199 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -82,7 +82,7 @@ public function to($route, $parameters = [], $absolute = false) // has been constructed, we'll make sure we don't have any missing parameters or we // will need to throw the exception to let the developers know one was not given. $uri = $this->addQueryString($this->url->format( - $root = $this->replaceRootParameters($route, $domain, $parameters), + $this->replaceRootParameters($route, $domain, $parameters), $this->replaceRouteParameters($route->uri(), $parameters) ), $parameters);