PHP Class yii\web\AssetManager

AssetManager is configured as an application component in Application by default. You can access that instance via Yii::$app->assetManager. You can modify its configuration by adding an array to your application config under components as shown in the following example: php 'assetManager' => [ 'bundles' => [ you can override AssetBundle configs here ], ] For more details and usage information on AssetManager, see the guide article on assets.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Component
ファイルを表示 Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$afterCopy a PHP callback that is called after a sub-directory or file is successfully copied. This option is used only when publishing a directory. The signature of the callback is the same as for [[beforeCopy]]. This is passed as a parameter afterCopy to [[\yii\helpers\FileHelper::copyDirectory()]].
$appendTimestamp whether to append a timestamp to the URL of every published asset. When this is true, the URL of a published asset may look like /path/to/asset?v=timestamp, where timestamp is the last modification time of the published asset file. You normally would want to set this property to true when you have enabled HTTP caching for assets, because it allows you to bust caching when the assets are updated.
$assetMap mapping from source asset files (keys) to target asset files (values). This property is provided to support fixing incorrect asset file paths in some asset bundles. When an asset bundle is registered with a view, each relative asset file in its [[AssetBundle::css|css]] and [[AssetBundle::js|js]] arrays will be examined against this map. If any of the keys is found to be the last part of an asset file (which is prefixed with [[AssetBundle::sourcePath]] if available), the corresponding value will replace the asset and be registered with the view. For example, an asset file my/path/to/jquery.js matches a key jquery.js. Note that the target asset files should be absolute URLs, domain relative URLs (starting from '/') or paths relative to [[baseUrl]] and [[basePath]]. In the following example, any assets ending with jquery.min.js will be replaced with jquery/dist/jquery.js which is relative to [[baseUrl]] and [[basePath]]. php [ 'jquery.min.js' => 'jquery/dist/jquery.js', ] You may also use aliases while specifying map value, for example: php [ 'jquery.min.js' => '@web/js/jquery/jquery.js', ]
$basePath the root directory storing the published asset files.
$baseUrl the base URL through which the published asset files can be accessed.
$beforeCopy a PHP callback that is called before copying each sub-directory or file. This option is used only when publishing a directory. If the callback returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the callback should be: function ($from, $to), where $from is the sub-directory or file to be copied from, while $to is the copy target. This is passed as a parameter beforeCopy to [[\yii\helpers\FileHelper::copyDirectory()]].
$bundles list of asset bundle configurations. This property is provided to customize asset bundles. When a bundle is being loaded by AssetManager::getBundle, if it has a corresponding configuration specified here, the configuration will be applied to the bundle. The array keys are the asset bundle names, which typically are asset bundle class names without leading backslash. The array values are the corresponding configurations. If a value is false, it means the corresponding asset bundle is disabled and AssetManager::getBundle should return null. If this property is false, it means the whole asset bundle feature is disabled and AssetManager::getBundle will always return null. The following example shows how to disable the bootstrap css file used by Bootstrap widgets (because you want to use your own styles): php [ 'yii\bootstrap\BootstrapAsset' => [ 'css' => [], ], ]
$dirMode the permission to be set for newly generated asset directories. This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
$fileMode the permission to be set for newly published asset files. This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.
$forceCopy 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. You may want to set this to be true during the development stage to make sure the published directory is always up-to-date. Do not set this to true on production servers as it will significantly degrade the performance.
$hashCallback a callback that will be called to produce hash for asset directory generation. The signature of the callback should be as follows: function ($path) where $path is the asset path. Note that the $path can be either directory where the asset files reside or a single file. For a CSS file that uses relative path in url(), the hash implementation should use the directory path of the file instead of the file path to include the relative asset files in the copying. If this is not set, the asset manager will use the default CRC32 and filemtime in the hash method. Example of an implementation using MD4 hash: php function ($path) { return hash('md4', $path); }
$linkAssets whether to use symbolic link to publish asset files. Defaults to false, meaning asset files are copied to [[basePath]]. Using symbolic links has the benefit that the published assets will always be consistent with the source assets and there is no copy operation required. This is especially useful during development. However, there are special requirements for hosting environments in order to use symbolic links. In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater. Moreover, some Web servers need to be properly configured so that the linked assets are accessible to Web users. For example, for Apache Web server, the following configuration directive should be added for the Web folder: apache Options FollowSymLinks

Public Methods

Method Description
getAssetPath ( AssetBundle $bundle, string $asset ) : string | false Returns the actual file path for the specified asset.
getAssetUrl ( AssetBundle $bundle, string $asset ) : string Returns the actual URL for the specified asset.
getBundle ( string $name, boolean $publish = true ) : AssetBundle Returns the named asset bundle.
getConverter ( ) : yii\web\AssetConverterInterface Returns the asset converter.
getPublishedPath ( string $path ) : string | false Returns the published path of a file path.
getPublishedUrl ( string $path ) : string | false Returns the URL of a published file path.
init ( ) Initializes the component.
publish ( string $path, array $options = [] ) : array Publishes a file or a directory.
setConverter ( array | yii\web\AssetConverterInterface $value ) Sets the asset converter.

Protected Methods

Method Description
hash ( string $path ) : string Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.
loadBundle ( string $name, array $config = [], boolean $publish = true ) : AssetBundle Loads asset bundle class by name
loadDummyBundle ( string $name ) : AssetBundle Loads dummy bundle by name
publishDirectory ( string $src, array $options ) : string[] Publishes a directory.
publishFile ( string $src ) : string[] Publishes a file.
resolveAsset ( AssetBundle $bundle, string $asset ) : string | boolean

Method Details

getAssetPath() public method

Returns the actual file path for the specified asset.
public getAssetPath ( AssetBundle $bundle, string $asset ) : string | false
$bundle AssetBundle the asset bundle which the asset file belongs to
$asset string the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]].
return string | false the actual file path, or `false` if the asset is specified as an absolute URL

getAssetUrl() public method

The actual URL is obtained by prepending either [[AssetBundle::$baseUrl]] or [[AssetManager::$baseUrl]] to the given asset path.
public getAssetUrl ( AssetBundle $bundle, string $asset ) : string
$bundle AssetBundle the asset bundle which the asset file belongs to
$asset string the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]].
return string the actual URL for the specified asset.

getBundle() public method

This method will first look for the bundle in [[bundles]]. If not found, it will treat $name as the class of the asset bundle and create a new instance of it.
public getBundle ( string $name, boolean $publish = true ) : AssetBundle
$name string the class name of the asset bundle (without the leading backslash)
$publish boolean whether to publish the asset files in the asset bundle before it is returned. If you set this false, you must manually call `AssetBundle::publish()` to publish the asset files.
return AssetBundle the asset bundle instance

getConverter() public method

Returns the asset converter.
public getConverter ( ) : yii\web\AssetConverterInterface
return yii\web\AssetConverterInterface the asset converter.

getPublishedPath() public method

This method does not perform any publishing. It merely tells you if the file or directory is published, where it will go.
public getPublishedPath ( string $path ) : string | false
$path string directory or file path being published
return string | false string the published file path. False if the file or directory does not exist

getPublishedUrl() public method

This method does not perform any publishing. It merely tells you if the file path is published, what the URL will be to access it.
public getPublishedUrl ( string $path ) : string | false
$path string directory or file path being published
return string | false string the published URL for the file or directory. False if the file or directory does not exist.

hash() protected method

Generate a CRC32 hash for the directory path. Collisions are higher than MD5 but generates a much smaller hash string.
protected hash ( string $path ) : string
$path string string to be hashed.
return string hashed string.

init() public method

Initializes the component.
public init ( )

loadBundle() protected method

Loads asset bundle class by name
protected loadBundle ( string $name, array $config = [], boolean $publish = true ) : AssetBundle
$name string bundle name
$config array bundle object configuration
$publish boolean if bundle should be published
return AssetBundle

loadDummyBundle() protected method

Loads dummy bundle by name
protected loadDummyBundle ( string $name ) : AssetBundle
$name string
return AssetBundle

publish() public method

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: http://code.google.com/p/yii/issues/detail?id=2579
public publish ( string $path, array $options = [] ) : array
$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.
return array the path (directory or file path) and the URL that the asset is published as.

publishDirectory() protected method

Publishes a directory.
protected publishDirectory ( string $src, array $options ) : string[]
$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.
return string[] the path directory and the URL that the asset is published as.

publishFile() protected method

Publishes a file.
protected publishFile ( string $src ) : string[]
$src string the asset file to be published
return string[] the path and the URL that the asset is published as.

resolveAsset() protected method

protected resolveAsset ( AssetBundle $bundle, string $asset ) : string | boolean
$bundle AssetBundle
$asset string
return string | boolean

setConverter() public method

Sets the asset converter.
public setConverter ( array | yii\web\AssetConverterInterface $value )
$value array | yii\web\AssetConverterInterface the asset converter. This can be either an object implementing the [[AssetConverterInterface]], or a configuration array that can be used to create the asset converter object.

Property Details

$afterCopy public_oe property

a PHP callback that is called after a sub-directory or file is successfully copied. This option is used only when publishing a directory. The signature of the callback is the same as for [[beforeCopy]]. This is passed as a parameter afterCopy to [[\yii\helpers\FileHelper::copyDirectory()]].
public $afterCopy

$appendTimestamp public_oe property

whether to append a timestamp to the URL of every published asset. When this is true, the URL of a published asset may look like /path/to/asset?v=timestamp, where timestamp is the last modification time of the published asset file. You normally would want to set this property to true when you have enabled HTTP caching for assets, because it allows you to bust caching when the assets are updated.
Since: 2.0.3
public $appendTimestamp

$assetMap public_oe property

mapping from source asset files (keys) to target asset files (values). This property is provided to support fixing incorrect asset file paths in some asset bundles. When an asset bundle is registered with a view, each relative asset file in its [[AssetBundle::css|css]] and [[AssetBundle::js|js]] arrays will be examined against this map. If any of the keys is found to be the last part of an asset file (which is prefixed with [[AssetBundle::sourcePath]] if available), the corresponding value will replace the asset and be registered with the view. For example, an asset file my/path/to/jquery.js matches a key jquery.js. Note that the target asset files should be absolute URLs, domain relative URLs (starting from '/') or paths relative to [[baseUrl]] and [[basePath]]. In the following example, any assets ending with jquery.min.js will be replaced with jquery/dist/jquery.js which is relative to [[baseUrl]] and [[basePath]]. php [ 'jquery.min.js' => 'jquery/dist/jquery.js', ] You may also use aliases while specifying map value, for example: php [ 'jquery.min.js' => '@web/js/jquery/jquery.js', ]
public $assetMap

$basePath public_oe property

the root directory storing the published asset files.
public $basePath

$baseUrl public_oe property

the base URL through which the published asset files can be accessed.
public $baseUrl

$beforeCopy public_oe property

a PHP callback that is called before copying each sub-directory or file. This option is used only when publishing a directory. If the callback returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the callback should be: function ($from, $to), where $from is the sub-directory or file to be copied from, while $to is the copy target. This is passed as a parameter beforeCopy to [[\yii\helpers\FileHelper::copyDirectory()]].
public $beforeCopy

$bundles public_oe property

list of asset bundle configurations. This property is provided to customize asset bundles. When a bundle is being loaded by AssetManager::getBundle, if it has a corresponding configuration specified here, the configuration will be applied to the bundle. The array keys are the asset bundle names, which typically are asset bundle class names without leading backslash. The array values are the corresponding configurations. If a value is false, it means the corresponding asset bundle is disabled and AssetManager::getBundle should return null. If this property is false, it means the whole asset bundle feature is disabled and AssetManager::getBundle will always return null. The following example shows how to disable the bootstrap css file used by Bootstrap widgets (because you want to use your own styles): php [ 'yii\bootstrap\BootstrapAsset' => [ 'css' => [], ], ]
public $bundles

$dirMode public_oe property

the permission to be set for newly generated asset directories. This value will be used by PHP chmod() function. No umask will be applied. Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
public $dirMode

$fileMode public_oe property

the permission to be set for newly published asset files. This value will be used by PHP chmod() function. No umask will be applied. If not set, the permission will be determined by the current environment.
public $fileMode

$forceCopy public_oe property

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. You may want to set this to be true during the development stage to make sure the published directory is always up-to-date. Do not set this to true on production servers as it will significantly degrade the performance.
public $forceCopy

$hashCallback public_oe property

a callback that will be called to produce hash for asset directory generation. The signature of the callback should be as follows: function ($path) where $path is the asset path. Note that the $path can be either directory where the asset files reside or a single file. For a CSS file that uses relative path in url(), the hash implementation should use the directory path of the file instead of the file path to include the relative asset files in the copying. If this is not set, the asset manager will use the default CRC32 and filemtime in the hash method. Example of an implementation using MD4 hash: php function ($path) { return hash('md4', $path); }
Since: 2.0.6
public $hashCallback

$linkAssets public_oe property

whether to use symbolic link to publish asset files. Defaults to false, meaning asset files are copied to [[basePath]]. Using symbolic links has the benefit that the published assets will always be consistent with the source assets and there is no copy operation required. This is especially useful during development. However, there are special requirements for hosting environments in order to use symbolic links. In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater. Moreover, some Web servers need to be properly configured so that the linked assets are accessible to Web users. For example, for Apache Web server, the following configuration directive should be added for the Web folder: apache Options FollowSymLinks
public $linkAssets