Skip to content

AssetManager ​

Type
Class
Namespace
craft\web
Inherits
craft\web\AssetManager » yii\web\AssetManager » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

View source

Public Properties ​

PropertyDescription
afterCopycallable, null – A PHP callback that is called after a sub-directory or file is successfully copied.
appendTimestampboolean – Whether to append a timestamp to the URL of every published asset.
assetMapstring[] – Mapping from source asset files (keys) to target asset files (values).
basePathstring – The root directory storing the published asset files.
baseUrlstring – The base URL through which the published asset files can be accessed.
beforeCopycallable, null – A PHP callback that is called before copying each sub-directory or file.
behaviorsyii\base\Behavior – List of behaviors attached to this component.
bundlesarray, false – List of asset bundle configurations.
cacheSourcePathsboolean – Whether asset source paths should be cached for subsequent requests.
converteryii\web\AssetConverterInterface – The asset converter.
dirModeinteger – The permission to be set for newly generated asset directories.
fileModeinteger, null – The permission to be set for newly published asset files.
forceCopyboolean – Whether the directory being published should be copied even if it is found in the target directory.
hashCallbackcallable, null – A callback that will be called to produce hash for asset directory generation.
linkAssetsboolean – Whether to use symbolic link to publish asset files.

cacheSourcePaths ​

Type
boolean
Default value
true
Since
4.5.10

Whether asset source paths should be cached for subsequent requests.

See also hash()

View source

Public Methods ​

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
checkBasePathPermission()Check whether the basePath exists and is writeable.
className()Returns the fully qualified name of this class.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
getActualAssetUrl()Returns the actual URL for the specified asset. Without parameters.
getAssetPath()Returns the actual file path for the specified asset.
getAssetUrl()
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getBundle()Returns the named asset bundle.
getCacheKeyForPathHash()Get the cache key for a given asset hash
getConverter()Returns the asset converter.
getPublishedPath()Returns the published path of a file path.
getPublishedUrl()Returns the URL of a published file/directory path.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the component.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
publish()Publishes a file or a directory.
setConverter()Sets the asset converter.
trigger()Triggers an event.

getAssetUrl() ​

View source

Arguments ​

  • $bundle
  • $asset
  • $appendTimestamp

getCacheKeyForPathHash() ​

Get the cache key for a given asset hash

View source

Arguments ​

Returns ​

string

getPublishedUrl() ​

Returns the URL of a published file/directory path.

View source

Arguments ​

  • $path (string) – Directory or file path being published
  • $publish (boolean) – Whether the directory or file should be published, if not already
  • $filePath (string, null) – A file path, relative to $sourcePath if $sourcePath is a directory, that should be appended to the returned URL.

Returns ​

string, false – The published URL for the file or directory, or false if $publish is false and the file or directory does not exist

publish() ​

Publishes a file or a directory.

This method will copy the specified file or directory to basePath so that it can be accessed via the Web server.

If the asset is a file, its file modification time will be checked to avoid unnecessary file copying.

If the asset is a directory, all files and subdirectories under it will be published recursively. Note, in case $forceCopy is false the method only checks the existence of the target directory to avoid repetitive copying (which is very expensive).

By default, when publishing a directory, subdirectories and files whose name starts with a dot "." will NOT be published. If you want to change this behavior, you may specify the "beforeCopy" option as explained in the $options parameter.

Note: On rare scenario, a race condition can develop that will lead to a one-time-manifestation of a non-critical problem in the creation of the directory that holds the published assets. This problem can be avoided altogether by 'requesting' in advance all the resources that are supposed to trigger a 'publish()' call, and doing that in the application deployment phase, before system goes live. See more in the following discussion: https://code.google.com/archive/p/yii/issues/2579

View source

Arguments ​

  • $path (string) – The asset (file or directory) to be published

  • $options (array) – The options to be applied when publishing a directory. The following options are supported:

  • only: array, list of patterns that the file paths should match if they want to be copied.

  • except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.

  • caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.

  • beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. This overrides beforeCopy if set.

  • afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This overrides afterCopy if set.

  • forceCopy: boolean, whether the directory being published should be copied even if it is found in the target directory. This option is used only when publishing a directory. This overrides forceCopy if set.

Returns ​

array – The path (directory or file path) and the URL that the asset is published as.

Throws ​

Protected Methods ​

MethodDescription
hash()Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.
loadBundle()Loads asset bundle class by name.
loadDummyBundle()Loads dummy bundle by name.
publishDirectory()Publishes a directory.
publishFile()Publishes a file.
resolveAsset()

hash() ​

Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.

View source

Arguments ​

  • $path (string) – String to be hashed.

Returns ​

string – Hashed string.

publishDirectory() ​

Publishes a directory.

View source

Arguments ​

  • $src (string) – The asset directory to be published

  • $options (array) – The options to be applied when publishing a directory. The following options are supported:

  • only: array, list of patterns that the file paths should match if they want to be copied.

  • except: array, list of patterns that the files or directories should match if they want to be excluded from being copied.

  • caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.

  • beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. This overrides beforeCopy if set.

  • afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied. This overrides afterCopy if set.

  • forceCopy: boolean, whether the directory being published should be copied even if it is found in the target directory. This option is used only when publishing a directory. This overrides forceCopy if set.

Returns ​

string[] – The path directory and the URL that the asset is published as.

Throws ​

publishFile() ​

Publishes a file.

View source

Arguments ​

  • $src (string) – The asset file to be published

Returns ​

string[] – The path and the URL that the asset is published as.

Throws ​

Constants ​

ConstantDescription
CACHE_TAG