0

I'm trying to include a css file located in frontend/web/css to backend main layout. It is working for backend but produce error for frontend. frontend web url is staging.example.com and backend web url is backend.example.com.

<?php
namespace frontend\assets;
use yii\web\AssetBundle;

class IconAsset extends AssetBundle
{
    public $sourcePath = '@frontend/web';
    public $css = [
        'css/icon.css',
    ];
}
?>

Code inside frontend layout main.php

use frontend\assets\IconAsset;
IconAsset::register($this);

Code inside backend layout main.php

use frontend\assets\IconAsset;
IconAsset::register($this);

1 Answer 1

0

You need to create the Custom Asset Bundle in frontend as follows

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

        class CustomAsset extends AssetBundle{
            public $basePath = '@webroot';
            public $baseUrl = '@web';
            public $css = [
                'css/custom.css',
                'css/another_css.css'
            ];
        }`

Put above code in frontend web accessible folder and register this asset into bakend main layout file as follows

use frontend\assets\CustomAsset;
use Yii;

$bundle = CustomAsset::register(Yii::$app->view);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your comment. My code working for backend but give error when I try to register IconAsset in frontend main layout it gives error. Please tell me how to use this in frontend main layout.
refer this answer given by Carlos Manzo
I tried your code but got Cross origin request error since my fronted (staging.example.com) and backend (backend.example.com) have different subdomain

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.