Home » Blog » Yii2.0 framework » How to Clear the Assets Cache in Yii2

How to Clear the Assets Cache in Yii2

Updated:   Yii2.0 framework 1 min read

Your support helps keep this blog running! Secure payments via Paypal and Stripe.


With Yii2, it comes with the asset management calls asset bundleAn asset bundle is simply a collection of assets located in a directory. When you register an asset bundle in a view, it will include the CSS and JavaScript files in the bundle in the rendered Web page.

Below is my asset bundle setting in my application. I use the advanced template from Yii2, and I use the frontend for my application(backend, console, and frontend folders are generated from the advanced template from Yii2). The code snippet below is in the AppAsset.php you can name the file whatever you need. The location is the same as the namespace of the Yii2 standard.

<?php

namespace frontend\assets;
use yii\web\AssetBundle;

/**
 * Main frontend application asset bundle.
 */
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',
    ];
}
?>

I set up Gulp for compiling the Sass file and put the generated CSS, JS, or fonts file in frontend/web/assets. The frontend/web is my root application. That means Yii will cache all CSS, fonts, and JS files in frontend/web/assets. Every time I run gulp to change the style or JS script, or fonts file, I have to delete all cache files in frontend/web/assets, then refresh my browser(Ctrl+F5 for Windows) to see the changes. And that’s it.


Your support helps keep this blog running! Secure payments via Paypal and Stripe.


Share this:
Senior WordPress Developer (Freelancer)

Senior WordPress Developer (Freelancer)

I’m a professional WordPress and WooCommerce developer based in Chiang Mai, Thailand, with over a decade of experience creating fast, secure, and scalable websites. From custom themes and plugins to full WooCommerce stores, I help businesses build a strong and reliable online presence. Need a freelance WordPress developer you can count on? View my portfolio or get in touch to discuss your project.