Proper way to work with assets in Yii
Tue May 02, 2017 · 541 words

Did you notice? Yii creates strange set of directories names like 8523d23 or 10s89b92 in assets directory, and this even happens at runtime.

Yii Assets

Many applications are entirely self contained, and there’s no trouble or conflict putting necessary resources (images, CSS files, Javascript files, etc.) under the webroot:

and so on. If a module needs to add one more resources, it’s added directly and referenced by the path down from webroot.

But if one creates a module intended for widespread reuse elsewhere, naming conflicts start to emerge: How do you insure that your filename css/foo.css won’t conflict with some unrelated application’s attempt to use a file of the same name? Likewise with Javascript, images, and all the rest. This gets more difficult the more popular the module gets: it’s going to eventually conflict with something.

Asset Manager

Yii addresses this by use of CAssetManager, available as Yii::app()->assetManager, which can take a module’s private resource files and automatically publish them to the web-visible area (typically, webroot/assets/) on demand, giving the assets a non-conflicting unique name that can be later referenced in the code.

Module’s resources will be used first time once publishing is done and returning a URL to the application to use when generating HTML.

By calling Yii::app()->assetManager->publish() some stylesheets, images, scripts, or even entire directories can be put into a web-visible folder.

pager.css and other non-familiar files are produced by widgets (CLinkPager for example) and other components (such as CClientScript publishes jQuery whenever you need that).

During deployment, this folder should be empty, but it doesn’t really matter.

Adding plugins should never be done through framework folders. You can place them either in components dir and publish it runtime if necessary, or into any other convenient visible directory (like as images or css).

Example

To embed jquery.charcounter.js, put it in components directory, then call

Yii::app()->clientScript->registerScriptFile(
    Yii::app()->assetManager->publish(
        Yii::getPathOfAlias('application.components').'/jquery.charcounter.js'
    ),
    CClientScript::POS_END
);

Regarding weird folder names, I firmly believe they are unique hashes (or part of), so they can be differentiated if application uses several extensions.

Maintenance

I hope this article will help you in understanding one of the core area of Yii Framework. I will be writing few more articles on Yii as it is one of my favourite MVC framework. Write me if you have any queries regarding this or simply comment below.


back · posts · who is yuvi? · contact · home