Skip to content
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
24 changes: 24 additions & 0 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,24 @@ public function forceHttps($force = true)
}
}

/**
* Set the URL origin for all generated URLs.
*
* @param string|null $root
* @return void
*/
public function useOrigin(?string $root)
{
$this->forceRootUrl($root);
}

/**
* Set the forced root URL.
*
* @param string|null $root
* @return void
*
* @deprecated Use useOrigin
*/
public function forceRootUrl($root)
{
Expand All @@ -755,6 +768,17 @@ public function forceRootUrl($root)
$this->cachedRoot = null;
}

/**
* Set the URL origin for all generated asset URLs.
*
* @param string|null $root
* @return void
*/
public function useAssetOrigin(?string $root)
{
$this->assetRoot = $root ? rtrim($root, '/') : null;
}

/**
* Set a callback to be used to format the host of generated URLs.
*
Expand Down
26 changes: 22 additions & 4 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,18 +684,36 @@ public function testUrlGenerationThrowsExceptionForMissingParametersWithMeaningf
$url->route('foo', $parameters);
}

public function testForceRootUrl()
public function testSetAssetUrl()
{
$url = new UrlGenerator(
$routes = new RouteCollection,
Request::create('http://www.foo.com/')
);

$url->forceRootUrl('https://www.bar.com');
$url->useOrigin('https://www.bar.com');
$this->assertSame('http://www.bar.com/foo/bar', $url->to('foo/bar'));
$this->assertSame('http://www.bar.com/foo/bar', $url->asset('foo/bar'));

$url->useAssetOrigin('https://www.foo.com');
$this->assertNotSame('https://www.foo.com/foo/bar', $url->to('foo/bar'));
$this->assertSame('https://www.foo.com/foo/bar', $url->asset('foo/bar'));
$this->assertSame('https://www.foo.com/foo/bar', $url->asset('foo/bar', true));
$this->assertSame('https://www.foo.com/foo/bar', $url->asset('foo/bar', false));
}

public function testUseRootUrl()
{
$url = new UrlGenerator(
$routes = new RouteCollection,
Request::create('http://www.foo.com/')
);

$url->useOrigin('https://www.bar.com');
$this->assertSame('http://www.bar.com/foo/bar', $url->to('foo/bar'));

// Ensure trailing slash is trimmed from root URL as UrlGenerator already handles this
$url->forceRootUrl('http://www.foo.com/');
$url->useOrigin('http://www.foo.com/');
$this->assertSame('http://www.foo.com/bar', $url->to('/bar'));

/*
Expand All @@ -712,7 +730,7 @@ public function testForceRootUrl()

$this->assertSame('https://www.foo.com/foo', $url->route('plain'));

$url->forceRootUrl('https://www.bar.com');
$url->useOrigin('https://www.bar.com');
$this->assertSame('https://www.bar.com/foo', $url->route('plain'));
}

Expand Down