How to use the assets in Yii Framework 2.0

In this tutorial I will explain to you how you can use the assets in Yii Framework 2.0. An asset in Yii is a file that may be referenced in a Web page. It can be a CSS file, a JavaScript file, an image or video file, etc.

If you want to create a new asset for a CSS file, a JavaScript file, an image or video file, go to your G:\xampp\htdocs\yii\assets folder and create a new folder with this content:

<?php

namespace app\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}

Rename the AppAsset  class with the name of your created file. Also, you must edit this variable:

public $css = [
'css/site.css',
];

with the name of your Css file. If you have a Js file, edit this variable:

public $js = [
];

Next, go to your G:\xampp\htdocs\yii\views\layouts\main.php file and add this code at the top:

use app\assets\AppAsset;

Edit AppAsset with the name of your class. Next add this code AppAsset::register($this); bellow to register your asset. Edit  AppAsset with the name of your class.

In this way you can use the Assets in Yii 2.0.

Video tutorial:









Leave a Comment