UrlPackage
extends Package
in 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.
Tags
Table of Contents
- $baseUrls : mixed
- $context : mixed
- $sslPackage : mixed
- $versionStrategy : mixed
- __construct() : mixed
- getBaseUrl() : string
- Returns the base URL for a path.
- getUrl() : string
- Returns an absolute or root-relative public path.
- getVersion() : string
- Returns the asset version for an asset.
- chooseBaseUrl() : int
- Determines which base URL to use for the given path.
- getContext() : ContextInterface
- getVersionStrategy() : VersionStrategyInterface
- isAbsoluteUrl() : bool
- getSslUrls() : mixed
Properties
$baseUrls
private
mixed
$baseUrls
= []
$context
private
mixed
$context
$sslPackage
private
mixed
$sslPackage
$versionStrategy
private
mixed
$versionStrategy
Methods
__construct()
public
__construct(string|array<string|int, string> $baseUrls, VersionStrategyInterface $versionStrategy[, ContextInterface $context = null ]) : mixed
Parameters
- $baseUrls : string|array<string|int, string>
-
Base asset URLs
- $versionStrategy : VersionStrategyInterface
- $context : ContextInterface = null
Return values
mixed —getBaseUrl()
Returns the base URL for a path.
public
getBaseUrl(string $path) : string
Parameters
- $path : string
Return values
string —The base URL
getUrl()
Returns an absolute or root-relative public path.
public
getUrl(mixed $path) : string
Parameters
- $path : mixed
-
A path
Return values
string —The public path
getVersion()
Returns the asset version for an asset.
public
getVersion(mixed $path) : string
Parameters
- $path : mixed
-
A path
Return values
string —The version string
chooseBaseUrl()
Determines which base URL to use for the given path.
protected
chooseBaseUrl(string $path) : int
Override this method to change the default distribution strategy. This method should always return the same base URL index for a given path.
Parameters
- $path : string
Return values
int —The base URL index for the given path
getContext()
protected
getContext() : ContextInterface
Return values
ContextInterface —getVersionStrategy()
protected
getVersionStrategy() : VersionStrategyInterface
Return values
VersionStrategyInterface —isAbsoluteUrl()
protected
isAbsoluteUrl(mixed $url) : bool
Parameters
- $url : mixed
Return values
bool —getSslUrls()
private
getSslUrls(array<string|int, mixed> $urls) : mixed
Parameters
- $urls : array<string|int, mixed>