Yii2 - app assets
Solution 1
You should simply try:
class PrologueAssets extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
...
];
public $js = [
...
];
}
http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
PS : assets management is currently being refactored : https://github.com/yiisoft/yii2/pull/4855
Solution 2
I wanted to add two files,one under frontend/web/css/custom.css
and frontend/web/js/custom.js
I just found the frontend/assets/AppAsset.php and added them directly and the two files are loaded for every page i load.
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
'css/custom.css',
];
public $js = [
'js/custom.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
Related videos on Youtube
Joe
Web, desktop and DB developer. Passionate about databases. Very happy to do anything in C#. Enthusiast of logic, objectivity, patterns and abstraction!
Updated on September 16, 2022Comments
-
Joe over 1 year
Yii is great but assets is the thing that was always strange for me (since the 1.1 version). I'm using Yii2 currently with Advanced app template. I want to register some css/js files in frontend
main
layout view (trying to use HTML5UP Prologue Template). How it can be done? I placed mycss
files underfrontend/web/css
directory,js
underfrontend/web/js
and layout images underfrontend/web/images
dir.Under
frontend/assets
directory I createdPrologueAssets
class like this:namespace frontend\assets; use yii\web\AssetBundle; class PrologueAssets extends AssetBundle { public $sourcePath = "@webroot"; public $css = [ 'css/skel.css', 'css/style.css', 'css/style-wide.css', ]; public $js = [ 'js/jquery.min.js', 'js/jquery.scrolly.min.js', 'js/jquery.scrollzer.min.js', 'js/skel.min.js', 'js/skel-layers.min.js', 'js/init.js', ]; static function register($view) { die(self::sourcePath); parent::register($view); } }
Unfortunately none of these files are registered. How to do that?
Little second question - how to register css files only if eg IE8 or IE9 is detected?
-
Prabowo MurtiWhat happens when you include the CSS and JS files on
AppAsset.php
(instead ofPrologueAssets.php
? -
JoeI made a workaround - included the files in view using
Url::base()
to generate base url and by addingcss/...
orjs/...
to the end. I always thought assets are to complicated in Yii. -
Joe@PrabowoMurti - that was interesting. It tried to include the files but not in relation to base url but the current so it was working only on homepage. Pretty weird, ecpesially when at the same time the default yiis
site.css
is loaded properly.
-