class UrlPackage extends Package

Package that adds a base URL to asset URLs in addition to a version.

The package allows to use more than one base URLs in which case it randomly chooses one for each asset; it also guarantees that any given path will always use the same base URL to be nice with HTTP caching mechanisms.

When the request context is available, this package can choose the best base URL to use based on the current request scheme:

  • For HTTP request, it chooses between all base URLs;
  • For HTTPs requests, it chooses between HTTPs base URLs and relative protocol URLs or falls back to any base URL if no secure ones are available.

Methods

__construct(string|string[] $baseUrls, VersionStrategyInterface $versionStrategy, ContextInterface $context = null)

No description

string
getVersion(string $path)

Returns the asset version for an asset.

from Package
string
getUrl(string $path)

Returns an absolute or root-relative public path.

getContext()

No description

from Package
isAbsoluteUrl($url)

No description

from Package
string
getBaseUrl(string $path)

Returns the base URL for a path.

int
chooseBaseUrl(string $path)

Determines which base URL to use for the given path.

Details

at line 46
__construct(string|string[] $baseUrls, VersionStrategyInterface $versionStrategy, ContextInterface $context = null)

Parameters

string|string[] $baseUrls Base asset URLs
VersionStrategyInterface $versionStrategy
ContextInterface $context

in Package at line 38
string getVersion(string $path)

Returns the asset version for an asset.

Parameters

string $path A path

Return Value

string The version string

at line 72
string getUrl(string $path)

Returns an absolute or root-relative public path.

Parameters

string $path A path

Return Value

string The public path

in Package at line 58
protected ContextInterface getContext()

Return Value

ContextInterface

in Package at line 66
protected VersionStrategyInterface getVersionStrategy()

in Package at line 71
protected isAbsoluteUrl($url)

Parameters

$url

at line 102
string getBaseUrl(string $path)

Returns the base URL for a path.

Parameters

string $path

Return Value

string The base URL

at line 121
protected int chooseBaseUrl(string $path)

Determines which base URL to use for the given path.

Override this method to change the default distribution strategy. This method should always return the same base URL index for a given path.

Parameters

string $path

Return Value

int The base URL index for the given path