From b35e734eeb2472028b66b72f5fba40eac3dc0c3b Mon Sep 17 00:00:00 2001 From: Marco Rieser Date: Tue, 25 Feb 2025 20:29:27 +0100 Subject: [PATCH] [5.x] Asset Container returns relative url for same site (#11372) Co-authored-by: Jason Varga --- src/Assets/AssetContainer.php | 5 ++++- tests/Assets/AssetContainerTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index 29765fce49..86263865e7 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -27,6 +27,7 @@ use Statamic\Facades\Stache; use Statamic\Facades\URL; use Statamic\Support\Arr; +use Statamic\Support\Str; use Statamic\Support\Traits\FluentlyGetsAndSets; class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract, Augmentable @@ -138,7 +139,9 @@ public function url() return null; } - $url = rtrim($this->disk()->url('/'), '/'); + $url = (string) Str::of($this->disk()->url('/')) + ->rtrim('/') + ->after(config('app.url')); return ($url === '') ? '/' : $url; } diff --git a/tests/Assets/AssetContainerTest.php b/tests/Assets/AssetContainerTest.php index 507c735879..9bc8f92ffe 100644 --- a/tests/Assets/AssetContainerTest.php +++ b/tests/Assets/AssetContainerTest.php @@ -137,6 +137,21 @@ public function it_gets_the_url_from_the_disk_config_when_its_relative() $this->assertEquals('http://localhost/container', $container->absoluteUrl()); } + #[Test] + public function it_gets_the_url_from_the_disk_config_when_its_app_url() + { + config(['filesystems.disks.test' => [ + 'driver' => 'local', + 'root' => __DIR__.'/__fixtures__/container', + 'url' => 'http://localhost/container', + ]]); + + $container = (new AssetContainer)->disk('test'); + + $this->assertEquals('/container', $container->url()); + $this->assertEquals('http://localhost/container', $container->absoluteUrl()); + } + #[Test] public function its_private_if_the_disk_has_no_url() {