Skip to content

Commit b544b83

Browse files
[11.x] Add a setAssetRoot method to the UrlGenerator class (#54530)
* Add a setAssetUrl method to the UrlGenerator class * Change the method name to setAssetRoot to match property * formatting * fix --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d0ac2bf commit b544b83

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,24 @@ public function forceHttps($force = true)
742742
}
743743
}
744744

745+
/**
746+
* Set the URL origin for all generated URLs.
747+
*
748+
* @param string|null $root
749+
* @return void
750+
*/
751+
public function useOrigin(?string $root)
752+
{
753+
$this->forceRootUrl($root);
754+
}
755+
745756
/**
746757
* Set the forced root URL.
747758
*
748759
* @param string|null $root
749760
* @return void
761+
*
762+
* @deprecated Use useOrigin
750763
*/
751764
public function forceRootUrl($root)
752765
{
@@ -755,6 +768,17 @@ public function forceRootUrl($root)
755768
$this->cachedRoot = null;
756769
}
757770

771+
/**
772+
* Set the URL origin for all generated asset URLs.
773+
*
774+
* @param string|null $root
775+
* @return void
776+
*/
777+
public function useAssetOrigin(?string $root)
778+
{
779+
$this->assetRoot = $root ? rtrim($root, '/') : null;
780+
}
781+
758782
/**
759783
* Set a callback to be used to format the host of generated URLs.
760784
*

tests/Routing/RoutingUrlGeneratorTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,18 +684,36 @@ public function testUrlGenerationThrowsExceptionForMissingParametersWithMeaningf
684684
$url->route('foo', $parameters);
685685
}
686686

687-
public function testForceRootUrl()
687+
public function testSetAssetUrl()
688688
{
689689
$url = new UrlGenerator(
690690
$routes = new RouteCollection,
691691
Request::create('http://www.foo.com/')
692692
);
693693

694-
$url->forceRootUrl('https://www.bar.com');
694+
$url->useOrigin('https://www.bar.com');
695+
$this->assertSame('http://www.bar.com/foo/bar', $url->to('foo/bar'));
696+
$this->assertSame('http://www.bar.com/foo/bar', $url->asset('foo/bar'));
697+
698+
$url->useAssetOrigin('https://www.foo.com');
699+
$this->assertNotSame('https://www.foo.com/foo/bar', $url->to('foo/bar'));
700+
$this->assertSame('https://www.foo.com/foo/bar', $url->asset('foo/bar'));
701+
$this->assertSame('https://www.foo.com/foo/bar', $url->asset('foo/bar', true));
702+
$this->assertSame('https://www.foo.com/foo/bar', $url->asset('foo/bar', false));
703+
}
704+
705+
public function testUseRootUrl()
706+
{
707+
$url = new UrlGenerator(
708+
$routes = new RouteCollection,
709+
Request::create('http://www.foo.com/')
710+
);
711+
712+
$url->useOrigin('https://www.bar.com');
695713
$this->assertSame('http://www.bar.com/foo/bar', $url->to('foo/bar'));
696714

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

701719
/*
@@ -712,7 +730,7 @@ public function testForceRootUrl()
712730

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

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

0 commit comments

Comments
 (0)