AssetController
API reference › yii › console › controllers › AssetController
Allows you to combine and compress your JavaScript and CSS files.
Usage:
-
Create a configuration file using the
templateaction:yii asset/template /path/to/myapp/config.php
-
Edit the created config file, adjusting it for your web application needs.
-
Run the ‘compress’ action, using created config:
yii asset /path/to/myapp/config.php /path/to/myapp/config/assets_compressed.php
-
Adjust your web application config to use compressed assets.
Note: in the console environment some path aliases like @webroot and @web may not exist,
so corresponding paths inside the configuration should be specified directly.
Note: by default this command relies on an external tools to perform actual files compression,
check $jsCompressor and $cssCompressor for more details.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$action | yii\<static>| | The action that is currently being executed. Default: null. | yii\ |
$assetManager | yii\| | Asset manager instance or its array configuration. | AssetController |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$bundles | array | List of asset bundles to be compressed. Default: []. | AssetController |
$color | bool| | Whether to enable ANSI color in the output. Default: null. | yii\ |
$cssCompressor | string| | CSS file compressor. Default: 'java -jar yuicompressor.jar --type css {from} -o {to}'. | AssetController |
$defaultAction | string | Controller default action ID. Default: 'compress'. | AssetController |
$deleteSource | bool | Whether to delete asset source files after compression. Default: false. Available since 2.0.10. | AssetController |
$help | bool | Whether to display help information about current command. Default: false. Available since 2.0.10. | yii\ |
$helpSummary read-only | string | The one-line short summary describing this controller. | yii\ |
$id | string | The ID of this controller. Default: null. | yii\ |
$interactive | bool | Whether to run the command interactively. Default: true. | yii\ |
$jsCompressor | string| | JavaScript file compressor. Default: 'java -jar compiler.jar --js {from} --js_output_file {to}'. | AssetController |
$layout | string| | The name of the layout to be applied to this controller’s views. Default: null. | yii\ |
$module | yii\ | The module that this controller belongs to. Default: null. | yii\ |
$modules read-only | yii\[] | All ancestor modules that this controller is located within. | yii\ |
$passedOptions read-only | array | The names of the options passed during execution. | yii\ |
$passedOptionValues read-only | array | The properties corresponding to the passed options. | yii\ |
$request | yii\ | The request object. | yii\ |
$response | yii\ | The response object. | yii\ |
$route read-only | string | The route (module ID, controller ID and action ID) of the current request. | yii\ |
$silentExitOnException | bool| | If true - script finish with ExitCode::OK in case of exception. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36. | yii\ |
$targets | array | List of asset bundles, which represents output compressed files. Default: []. | AssetController |
$uniqueId read-only | string | The controller ID that is prefixed with the module ID (if any). | yii\ |
$view | yii\|yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath | string | The directory containing the view files for this controller. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | yii\ | |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
actionCompress() | Combines and compresses the asset files according to the given configuration. | AssetController |
actions() | Declares external actions for the controller. | yii\ |
actionTemplate() | Creates template of configuration file for actionCompress(). | AssetController |
afterAction() | This method is invoked right after an action is executed. | yii\ |
ansiFormat() | Formats a string with ANSI codes. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeAction() | This method is invoked right before an action is executed. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
bindActionParams() | Binds the parameters to the action. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
combineCssFiles() | Combines CSS files into a single one. | AssetController |
combineJsFiles() | Combines JavaScript files into a single one. | AssetController |
confirm() | Asks user to confirm by typing y or n. | yii\ |
createAction() | Creates an action based on the given action ID. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
findLayoutFile() | Finds the applicable layout file. | yii\ |
getActionArgsHelp() | Returns the help information for the anonymous arguments for the action. | yii\ |
getActionHelp() | Returns the detailed help information for the specified action. | yii\ |
getActionHelpSummary() | Returns a one-line short summary describing the specified action. | yii\ |
getActionOptionsHelp() | Returns the help information for the options for the action. | yii\ |
getAssetManager() | Returns the asset manager instance. | AssetController |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getHelp() | Returns help information for this controller. | yii\ |
getHelpSummary() | Returns one-line short summary describing this controller. | yii\ |
getModules() | Returns all ancestor modules of this controller. | yii\ |
getOptionValues() | Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties. | yii\ |
getPassedOptions() | Returns the names of valid options passed during execution. | yii\ |
getPassedOptionValues() | Returns the properties corresponding to the passed options. | yii\ |
getRoute() | Returns the route of the current request. | yii\ |
getUniqueId() | Returns the unique ID of the controller. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing view files for this controller. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
isColorEnabled() | Returns a value indicating whether ANSI color is enabled. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
optionAliases() | Returns option alias names. | yii\ |
options() | Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name. | yii\ |
prompt() | Prompts the user for input and validates it. | yii\ |
render() | Renders a view and applies layout if available. | yii\ |
renderContent() | Renders a static string by applying a layout. | yii\ |
renderFile() | Renders a view file. | yii\ |
renderPartial() | Renders a view without applying layout. | yii\ |
run() | Runs a request specified in terms of a route. | yii\ |
runAction() | Runs an action with the specified action ID and parameters. | yii\ |
select() | Gives the user an option to choose from. Giving ’?’ as an input will show a list of options to choose from and their explanations. | yii\ |
setAssetManager() | Sets asset manager instance or configuration. | AssetController |
setView() | Sets the view object to be used by this controller. | yii\ |
setViewPath() | Sets the directory that contains the view files. | yii\ |
stderr() | Prints a string to STDERR. | yii\ |
stdout() | Prints a string to STDOUT. | yii\ |
trigger() | Triggers an event. | yii\ |
adjustCssUrl() protected | Adjusts CSS content allowing URL references pointing to the original resources. | AssetController |
adjustDependency() protected | Adjust dependencies between asset bundles in the way source bundles begin to depend on output ones. | AssetController |
bindInjectedParams() protected final | Fills parameters based on types and names in action method signature. | yii\ |
buildTarget() protected | Builds output asset bundle. | AssetController |
compressCssFiles() protected | Compresses given CSS files and combines them into the single one. | AssetController |
compressJsFiles() protected | Compresses given JavaScript files and combines them into the single one. | AssetController |
getActionMethodReflection() protected | yii\ | |
loadBundles() protected | Creates full list of source asset bundles. | AssetController |
loadConfiguration() protected | Applies configuration from the given file to self instance. | AssetController |
loadDependency() protected | Loads asset bundle dependencies recursively. | AssetController |
loadTargets() protected | Creates full list of output asset bundles. | AssetController |
parseDocCommentDetail() protected | Returns full description from the docblock. | yii\ |
parseDocCommentSummary() protected | Returns the first line of docblock. | yii\ |
parseDocCommentTags() protected | Parses the comment block into tags. | yii\ |
registerBundle() protected | Registers asset bundles including their dependencies. | AssetController |
saveTargets() protected | Saves new asset bundles configuration. | AssetController |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_ACTION | yii\ | An event raised right before executing a controller action. Event name: 'beforeAction'. | yii\ |
EVENT_AFTER_ACTION | yii\ | An event raised right after executing a controller action. Event name: 'afterAction'. | yii\ |
Property details
Section titled “Property details”$cssCompressor
Section titled “$cssCompressor ”public string|callable $cssCompressor = 'java -jar yuicompressor.jar --type css {from} -o {to}'CSS file compressor.
If a string, it is treated as shell command template, which should contain placeholders {from} - source file name - and {to} - output file name. Otherwise, it is treated as PHP callback, which should perform the compression.
Default value relies on usage of “YUI Compressor”
$deleteSource
Section titled “$deleteSource ”public bool $deleteSource = falseWhether to delete asset source files after compression.
This option affects only those bundles, which have AssetBundle::$sourcePath is set.
Since
2.0.10
$jsCompressor
Section titled “$jsCompressor ”public string|callable $jsCompressor = 'java -jar compiler.jar --js {from} --js_output_file {to}'JavaScript file compressor.
If a string, it is treated as shell command template, which should contain placeholders {from} - source file name - and {to} - output file name. Otherwise, it is treated as PHP callback, which should perform the compression.
Default value relies on usage of “Closure Compiler”
$targets
Section titled “$targets ”public array $targets = []List of asset bundles, which represents output compressed files.
You can specify the name of the output compressed file using ‘css’ and ‘js’ keys: For example:
'app\config\AllAsset' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [ ... ],]File names can contain placeholder “{hash}”, which will be filled by the hash of the resulting file.
You may specify several target bundles in order to compress different groups of assets. In this case you should use ‘depends’ key to specify, which bundles should be covered with particular target bundle. You may leave ‘depends’ to be empty for single bundle, which will compress all remaining bundles in this case. For example:
'allShared' => [ 'js' => 'js/all-shared-{hash}.js', 'css' => 'css/all-shared-{hash}.css', 'depends' => [ // Include all assets shared between 'backend' and 'frontend' 'yii\web\YiiAsset', 'app\assets\SharedAsset', ],],'allBackEnd' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [ // Include only 'backend' assets: 'app\assets\AdminAsset' ],],'allFrontEnd' => [ 'js' => 'js/all-{hash}.js', 'css' => 'css/all-{hash}.css', 'depends' => [], // Include all remaining assets],Method details
Section titled “Method details”actionCompress()
Section titled “actionCompress() ”public function actionCompress(string $configFile, string $bundleFile): mixedCombines and compresses the asset files according to the given configuration.
During the process new asset bundle configuration file will be created. You should replace your original asset bundle configuration with this file in order to use compressed files.
Parameters
| Parameter | Type | Description |
|---|---|---|
$configFile | string | Configuration file name. |
$bundleFile | string | Output asset bundles configuration file name. |
Returns
mixed
actionTemplate()
Section titled “actionTemplate() ”public function actionTemplate(string $configFile): intCreates template of configuration file for actionCompress().
Parameters
| Parameter | Type | Description |
|---|---|---|
$configFile | string | Output file name. |
Returns
int: CLI exit code
Throws
yii\console\Exception: on failure.
combineCssFiles()
Section titled “combineCssFiles() ”public function combineCssFiles(array $inputFiles, string $outputFile): mixedCombines CSS files into a single one.
Parameters
| Parameter | Type | Description |
|---|---|---|
$inputFiles | array | Source file names. |
$outputFile | string | Output file name. |
Returns
mixed
Throws
yii\console\Exception: on failure.
combineJsFiles()
Section titled “combineJsFiles() ”public function combineJsFiles(array $inputFiles, string $outputFile): mixedCombines JavaScript files into a single one.
Parameters
| Parameter | Type | Description |
|---|---|---|
$inputFiles | array | Source file names. |
$outputFile | string | Output file name. |
Returns
mixed
Throws
yii\console\Exception: on failure.
getAssetManager()
Section titled “getAssetManager() ”public function getAssetManager(): yii\web\AssetManagerReturns the asset manager instance.
Returns
yii\web\AssetManager: Asset manager instance.
Throws
yii\console\Exception: on invalid configuration.
setAssetManager()
Section titled “setAssetManager() ”public function setAssetManager(yii\web\AssetManager|array $assetManager): mixedSets asset manager instance or configuration.
Parameters
| Parameter | Type | Description |
|---|---|---|
$assetManager | yii\| | Asset manager instance or its array configuration. |
Returns
mixed
Throws
yii\console\Exception: on invalid argument type.
adjustCssUrl()
Section titled “adjustCssUrl() ”protected function adjustCssUrl(string $cssContent, string $inputFilePath, string $outputFilePath): stringAdjusts CSS content allowing URL references pointing to the original resources.
Parameters
| Parameter | Type | Description |
|---|---|---|
$cssContent | string | Source CSS content. |
$inputFilePath | string | Input CSS file name. |
$outputFilePath | string | Output CSS file name. |
Returns
string: Adjusted CSS content.
adjustDependency()
Section titled “adjustDependency() ”protected function adjustDependency(yii\web\AssetBundle[] $targets, yii\web\AssetBundle[] $bundles): yii\web\AssetBundle[]Adjust dependencies between asset bundles in the way source bundles begin to depend on output ones.
Parameters
| Parameter | Type | Description |
|---|---|---|
$targets | yii\[] | Output asset bundles. |
$bundles | yii\[] | Source asset bundles. |
Returns
yii\web\AssetBundle[]: Output asset bundles.
buildTarget()
Section titled “buildTarget() ”protected function buildTarget(yii\web\AssetBundle $target, string $type, yii\web\AssetBundle[] $bundles): mixedBuilds output asset bundle.
Parameters
| Parameter | Type | Description |
|---|---|---|
$target | yii\ | Output asset bundle |
$type | string | Either ‘js’ or ‘css’. |
$bundles | yii\[] | Source asset bundles. |
Returns
mixed
Throws
yii\console\Exception: on failure.
compressCssFiles()
Section titled “compressCssFiles() ”protected function compressCssFiles(array $inputFiles, string $outputFile): mixedCompresses given CSS files and combines them into the single one.
Parameters
| Parameter | Type | Description |
|---|---|---|
$inputFiles | array | List of source file names. |
$outputFile | string | Output file name. |
Returns
mixed
Throws
yii\console\Exception: on failure
compressJsFiles()
Section titled “compressJsFiles() ”protected function compressJsFiles(array $inputFiles, string $outputFile): mixedCompresses given JavaScript files and combines them into the single one.
Parameters
| Parameter | Type | Description |
|---|---|---|
$inputFiles | array | List of source file names. |
$outputFile | string | Output file name. |
Returns
mixed
Throws
yii\console\Exception: on failure
loadBundles()
Section titled “loadBundles() ”protected function loadBundles(string[] $bundles): yii\web\AssetBundle[]Creates full list of source asset bundles.
Parameters
| Parameter | Type | Description |
|---|---|---|
$bundles | string[] | List of asset bundle names |
Returns
yii\web\AssetBundle[]: List of source asset bundles.
loadConfiguration()
Section titled “loadConfiguration() ”protected function loadConfiguration(string $configFile): mixedApplies configuration from the given file to self instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
$configFile | string | Configuration file name. |
Returns
mixed
Throws
yii\console\Exception: on failure.
loadDependency()
Section titled “loadDependency() ”protected function loadDependency(yii\web\AssetBundle $bundle, array &$result): mixedLoads asset bundle dependencies recursively.
Parameters
| Parameter | Type | Description |
|---|---|---|
$bundle | yii\ | Bundle instance |
$result | array | Already loaded bundles list. |
Returns
mixed
Throws
yii\console\Exception: on failure.
loadTargets()
Section titled “loadTargets() ”protected function loadTargets(array $targets, yii\web\AssetBundle[] $bundles): yii\web\AssetBundle[]Creates full list of output asset bundles.
Parameters
| Parameter | Type | Description |
|---|---|---|
$targets | array | Output asset bundles configuration. |
$bundles | yii\[] | List of source asset bundles. |
Returns
yii\web\AssetBundle[]: List of output asset bundles.
Throws
yii\console\Exception: on failure.
registerBundle()
Section titled “registerBundle() ”protected function registerBundle(yii\web\AssetBundle[] $bundles, string $name, array &$registered): mixedRegisters asset bundles including their dependencies.
Parameters
| Parameter | Type | Description |
|---|---|---|
$bundles | yii\[] | Asset bundles list. |
$name | string | Bundle name. |
$registered | array | Stores already registered names. |
Returns
mixed
Throws
yii\console\Exception: if circular dependency is detected.
saveTargets()
Section titled “saveTargets() ”protected function saveTargets(yii\web\AssetBundle[] $targets, string $bundleFile): mixedSaves new asset bundles configuration.
Parameters
| Parameter | Type | Description |
|---|---|---|
$targets | yii\[] | List of asset bundles to be saved. |
$bundleFile | string | Output file name. |
Returns
mixed
Throws
yii\console\Exception: on failure.