22

For example, I created a new page, and I'd like to use, for example, backbone.js, custom css file and some collection of images. Where should I declare all this stuff in Yii2? I found the AppAsset.php module, but this is only for css/js files and I haven't noticed any changes when my css/js files and path were declared there:

class AppAsset extends AssetBundle {
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'js/jquery.mobile-1.4.2.min.css',
    ];
    public $js = [
        'js/jsquery-2.1.0.min.js',
        'js/jquery.mobile-1.4.2.min.js',
        'js/script.js',
    ];

    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

What am I doing wrong?

4
  • ...and I haven't noticed any changes when my css/js files and path were declared there well did you register the bundle?
    – Cthulhu
    Sep 5, 2014 at 13:28
  • Yes, in target .php module i write: use frontend\assets\AppAsset; AppAsset::register($this); and nothing changes. Sep 5, 2014 at 13:51
  • check the path you have in the AppAsset.php.Is it correct? Sep 6, 2014 at 4:49
  • Well, the structure of my project is following (approximately): assets are existing in several incarnations: @root/frontend/web/assets/[there are hash code like 4cd64e87]/[css, fonts, js]/ ; @root/frontend/web/css ; @root/frontend/web/js ; and I'm trying to figure out where to place my files. I placed my jquery and script.js files into web/js directory, but it had no much effect on view, where these files are registered and after reloading my page is still without styling, like it has no links to css file. Sep 6, 2014 at 11:07

4 Answers 4

31

It took me a while to figure it out, but below is the relevant part of the Yii2 source code

if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
    list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}

So Yii2 will publish assets only if $sourcePath is set, and $basePath and $baseUrl are not set(!). The latter tripped me up, and it looks like the same goes for you.

So I have this AppAsset, which duly publishes

use yii\web\AssetBundle;


class AppAsset extends AssetBundle
{
public $sourcePath = '@app/assets/app';

public $css = [
    'css/openbook.css',
    'fontello/css/fontello.css',
    'fontello/css/animation.css'
];
public $js = [
    'js/plug.openbook.js',
    'js/plug.interpret.js',
    'js/plug.drop.message.js'
];
public $depends = [
   // 'yii\web\YiiAsset', 
   // 'yii\bootstrap\BootstrapAsset',
];
} 

Of course, I have in the main layout

use frontend\assets\AppAsset;
...
AppAsset::register($this);
2
  • I would add this answer into "Golden recommendations for Yii2"! Jun 12, 2016 at 18:35
  • 1
    Well, your conclusion that Yii2 publishes assets only when sourcePath is set isn't correct. sourcePath is required when the assets aren't inside a web accessible folder. However if the assets are inside a web accessible folder then you don't need to set sourcePath, instead you need to set basePath and baseUrl. Jul 15, 2016 at 10:11
4

to use this AppAsset or any other you should register it in the view

use app\assets\AppAsset;
AppAsset::register($this);
0

At first, you should create SomeAsset class in your app/assets/ folder with your new js and css files. You can extend your AppAsset overloading its properties.

Next use this in SomeController

use Yii;
use app\assets\SomeAsset;

and in actionSome like this:

SomeAsset::register(Yii::$app->view);
1
  • Please post the SomeAsset file too. Mar 26, 2018 at 15:23
-4

From personal experience, assets are one of the parts of Yii that I found extremely frustrating.

It is hard to reliably find out where the file is going to be and the switching back and forth with debug mode on and off will create further frustration.

I suggest scrapping the asset handling and just keep all your JS files in a folder, then it can be included like this:

Yii::app()->clientScript->registerScriptFile('/js/jquery.jeditable.mini.js');
1
  • 1
    I am sorry, but your script seems to be referring to Yii 1.1 assets. Assets are handled much differently (as Bundles, which can state other required assets) in Yii2.
    – Snivs
    Jul 8, 2015 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.