Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put common resources in yii2 advanced?

I want to access my css and javascript files in both frontend and backend.

So, my question is where i put css and javascript files so they are accessible for both frontend and backend.

Or is there any way to reuse files i.e. i put my css file in web folder of backend and use it in frontend.

Thanks

like image 490
Yasin Patel Avatar asked Jun 02 '16 12:06

Yasin Patel


1 Answers

As it says here, you can make asset bundle in @common folder and set property $sourcePath, using an alias or absolute path to directory with your scripts and styles. The scripts directory may not be web-accessible.

Try something like this:

namespace common\assets;
use yii\web\AssetBundle;
/**
 * Main frontend application asset bundle.
 */
class CommonAsset extends AssetBundle
{
    public $sourcePath = '@common/scripts';
    public $css = [
    ];
    public $js = [
        'blabla.js'
    ];
    public $depends = [
    ];
}

And place your scripts to common/scripts. Also don't forget to register this asset in your layout or another view:

use common\assets\CommonAsset; 
CommonAsset::register($this);

Worked for me.

like image 118
Nastya Kizza Avatar answered Nov 16 '22 03:11

Nastya Kizza