If you want to ship assets (JavaScript, CSS files) from your Yii2 module to your view, you can do that elegant with a couple of simple steps.
/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER
The assets are at
/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER/assets
So the JavaScript files are at
/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER/assets/js
1., Folder structure
We assume that your Yii2 module is located at/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER
The assets are at
/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER/assets
So the JavaScript files are at
/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER/assets/js
2., Set an alias to your assets in your Module.php in your module (/yii-base-folder/modules/YOUR-YII-MODULE-FOLDER/Module.php)
3., Put your asset class into your assets folder to setup your "assets bundle"
We name a bunch of assets "assets bundle". Here you declare your files and make it accessible to your view.4., Now you can access your assets from your view
We assume that your view's index.php file is located at /yii-base-folder/modules/YOUR-MODULE/views/ANY-VIEWS-SUBFOLDER/index.php
5., Conclusion
Now Yii2 puts your CSS and JavaScript files nicely into your HTML. By default the JavaScript files will be put nicely into the footer of your HTML. Also the CSS and JavaScript files will be copied into your web-accessible assets folder. The users from your webpage will not be allowed to get info about your Yii project folder structure. So the path to your JavaScript file will look like that:
The path in your project's directory will be:
/YOUR-Yii2-PROJECT/web/assets/3598bbc6/js/YOUR-JavaScript-File.js
Also the PHP asset class will be copied from your module's folder:
/YOUR-Yii2-PROJECT/web/assets/3598bbc6/YourAssetsPhpClass.php
Don't forget to not modify the files at /YOUR-Yii2-PROJECT/web/assets/3598bbc6/ manually, because they're created automatically by the Yii2 framework.
Hi, nice post. Thanks for sharing. I followed your tutorial and afterwards I want to compress and minify the assets of the module. I follow the guide but have some problems with background images in the CSS. Could you help?
AntwortenLöschenThanks
Hi, thanks for your complement. You could consider compressing your background image with base64. Here's a discussion for this case:http://stackoverflow.com/questions/1124149/is-embedding-background-image-data-into-css-as-base64-good-or-bad-practice
AntwortenLöschenHi Peter,
AntwortenLöschenThanks for your reply. The problem I have is that the module has some images which are part of the module's assets. When I publish the assets, a folder is created (like assets/80f9c689) with the css, js and images. The CSS compressor (I'm using yui for CSS) replaces the URLs in the CSS with assets/80f9c689/images/icon.ong, which works well on my local environment. However, when I deploy to production, the server generates other folders for assets like assets/c65aa1d0. Since the CSS has references to the images generated in the local folders, the images are not displayed.
Any idea how to overcome this?
Thanks in advance for your help.
Hi,
AntwortenLöschensorry for the late reply.
It seems to be a Yui implementation issue. Why are you re-generating the assets in production?
It is done automatically by Yii, the web/assets is gitignored, each environment creates its own subdirectories. I still haven't found a solution to this
AntwortenLöschenSorry man, I've found everything about assets in the fantastic Yii2 guide for assets at http://www.yiiframework.com/doc-2.0/guide-structure-assets.html. May the guide will help you out?
AntwortenLöschen