Skip to content

[5.5] Allow to custom assets root URL #22372

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

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 19 additions & 6 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class UrlGenerator implements UrlGeneratorContract
*/
protected $forceScheme;

/**
* The assets URL root.
*
* @var string
*/
protected $assetsRoot;

/**
* A cached copy of the URL root for the current request.
*
Expand Down Expand Up @@ -217,12 +224,7 @@ public function asset($path, $secure = null)
return $path;
}

// Once we get the root URL, we will check to see if it contains an index.php
// file in the paths. If it does, we will remove it since it is not needed
// for asset paths, but only for routes to endpoints in the application.
$root = $this->formatRoot($this->formatScheme($secure));

return $this->removeIndex($root).'/'.trim($path, '/');
return $this->assetFrom($this->assetsRoot, $path, $secure);
}

/**
Expand Down Expand Up @@ -503,6 +505,17 @@ public function forceRootUrl($root)
$this->cachedRoot = null;
}

/**
* Set the root URL for assets.
*
* @param string|null $root
* @return void
*/
public function setAssetsRoot($root)
{
$this->assetsRoot = rtrim($root, '/') ?: null;
}

/**
* Set a callback to be used to format the host of generated URLs.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,24 @@ public function testForceRootUrl()
$this->assertEquals('https://www.bar.com/foo', $url->route('plain'));
}

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

$url->setAssetsRoot('https://www.bar.com');
$this->assertEquals('http://www.bar.com/foo/bar', $url->asset('foo/bar'));

// Ensure trailing slash is trimmed from root URL as UrlGenerator already handles this
$url->setAssetsRoot('//www.foo.com/');
$this->assertEquals('//www.foo.com/bar', $url->asset('/bar'));

$url->setAssetsRoot(null);
$this->assertEquals('http://www.foo.com/bar', $url->asset('bar'));
}

public function testPrevious()
{
$url = new UrlGenerator(
Expand Down