Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Fix relative route URL generation with custom host formatter #24051

Merged
merged 2 commits into from
Apr 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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