Skip to content

Commit

Permalink
[5.6] Fix relative route URL generation with custom host formatter (#…
Browse files Browse the repository at this point in the history
…24051)

* Fix relative route URL generation with custom host formatter

* Remove unused variable
  • Loading branch information
staudenmeir authored and taylorotwell committed Apr 29, 2018
1 parent b9383a6 commit 6656ed6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Routing/RouteUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
23 changes: 19 additions & 4 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 6656ed6

Please sign in to comment.