From c6c1b673aa0f53f65a06c7032712b12978ff1d6e Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 29 Apr 2018 10:09:49 +0200 Subject: [PATCH 1/2] Pass route to custom URL formatters --- src/Illuminate/Routing/RouteUrlGenerator.php | 3 ++- src/Illuminate/Routing/UrlGenerator.php | 7 ++++--- tests/Routing/RoutingUrlGeneratorTest.php | 8 ++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Routing/RouteUrlGenerator.php b/src/Illuminate/Routing/RouteUrlGenerator.php index b5a4c1cbf1ec..a3e91b1ec51a 100644 --- a/src/Illuminate/Routing/RouteUrlGenerator.php +++ b/src/Illuminate/Routing/RouteUrlGenerator.php @@ -83,7 +83,8 @@ public function to($route, $parameters = [], $absolute = false) // 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->replaceRouteParameters($route->uri(), $parameters) + $this->replaceRouteParameters($route->uri(), $parameters), + $route ), $parameters); if (preg_match('/\{.*?\}/', $uri)) { diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 7c7a417ed20e..a4b1a305d3c6 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -485,18 +485,19 @@ public function formatRoot($scheme, $root = null) * * @param string $root * @param string $path + * @param \Illuminate\Routing\Route|null $route * @return string */ - public function format($root, $path) + public function format($root, $path, $route = null) { $path = '/'.trim($path, '/'); if ($this->formatHostUsing) { - $root = call_user_func($this->formatHostUsing, $root); + $root = call_user_func($this->formatHostUsing, $root, $route); } if ($this->formatPathUsing) { - $path = call_user_func($this->formatPathUsing, $path); + $path = call_user_func($this->formatPathUsing, $path, $route); } return trim($root.$path, '/'); diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 766cade3a69b..09ac0f0aec60 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -59,12 +59,16 @@ public function testBasicGenerationWithFormatting() $route = new Route(['GET'], '/named-route', ['as' => 'plain']); $routes->add($route); - $url->formatPathUsing(function ($path) { + $url->formatPathUsing(function ($path, $route) { + if ($route) { + return '/something'.$path.'/'.$route->getName(); + } + return '/something'.$path; }); $this->assertEquals('http://www.foo.com/something/foo/bar', $url->to('foo/bar')); - $this->assertEquals('/something/named-route', $url->route('plain', [], false)); + $this->assertEquals('/something/named-route/plain', $url->route('plain', [], false)); } public function testBasicRouteGeneration() From a453b7f9a2397f76f5770f26c46a1bf1a5efff60 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 2 May 2018 12:30:29 +0200 Subject: [PATCH 2/2] Create a separate test --- tests/Routing/RoutingUrlGeneratorTest.php | 30 ++++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 09ac0f0aec60..cc574e142488 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -59,16 +59,34 @@ public function testBasicGenerationWithFormatting() $route = new Route(['GET'], '/named-route', ['as' => 'plain']); $routes->add($route); - $url->formatPathUsing(function ($path, $route) { - if ($route) { - return '/something'.$path.'/'.$route->getName(); - } - + $url->formatPathUsing(function ($path) { return '/something'.$path; }); $this->assertEquals('http://www.foo.com/something/foo/bar', $url->to('foo/bar')); - $this->assertEquals('/something/named-route/plain', $url->route('plain', [], false)); + $this->assertEquals('/something/named-route', $url->route('plain', [], false)); + } + + public function testUrlFormattersShouldReceiveTargetRoute() + { + $url = new UrlGenerator( + $routes = new RouteCollection, + $request = Request::create('http://abc.com/') + ); + + $namedRoute = new Route(['GET'], '/bar', ['as' => 'plain', 'root' => 'bar.com', 'path' => 'foo']); + $routes->add($namedRoute); + + $url->formatHostUsing(function ($root, $route) { + return $route ? 'http://'.$route->getAction('root') : $root; + }); + + $url->formatPathUsing(function ($path, $route) { + return $route ? '/'.$route->getAction('path') : $path; + }); + + $this->assertEquals('http://abc.com/foo/bar', $url->to('foo/bar')); + $this->assertEquals('http://bar.com/foo', $url->route('plain')); } public function testBasicRouteGeneration()