How to link CSS and JS file in theme layout file

I am trying to integrate a theme in Yii2. I have placed the theme in themes folder inside the basic folder. In the config/web.php file, I have inserted the following code under components array.




$config = [

    'id' => 'basic',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'components' => [

        'request' => [

            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation

            'cookieValidationKey' => 'bnfhxfasdf7324675@$%fg',

        ],

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

        'user' => [

            'identityClass' => 'app\models\User',

            'enableAutoLogin' => true,

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        'mailer' => [

            'class' => 'yii\swiftmailer\Mailer',

            // send all mails to a file by default. You have to set

            // 'useFileTransport' to false and configure a transport

            // for the mailer to send real emails.

            'useFileTransport' => true,

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'db' => require(__DIR__ . '/db.php'),

        'view' => [

            'theme' => [

                'basePath' => '@app/themes/flat',

                'baseUrl' => '@web/themes/flat',

                'pathMap' => [

                    '@app/views' => '@app/themes/flat/views',

                ],

            ],

        ],

    ],

    'params' => $params,

];


if (YII_ENV_DEV) {

    // configuration adjustments for 'dev' environment

    $config['bootstrap'][] = 'debug';

    $config['modules']['debug'] = [

        'class' => 'yii\debug\Module',

    ];


    $config['bootstrap'][] = 'gii';

    $config['modules']['gii'] = [

        'class' => 'yii\gii\Module',

    ];

}


return $config;



In the theme main.php layout file I am trying to link a css file using $this->theme->getBaseUrl(). But it is giving url as /yii2crm/basic/web/themes/flat/assets/css/bootstrap.min.css, where as it should give /yii2crm/basic/themes/flat/assets/css/bootstrap.min.css. The theme is within basic folder.

Please help.

Hi!

You should publish your css / js / gfx using a AssetBundle.

Read: http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

If you need more info I suggest to download themes from:

http://yii2.themefactory.net/

And look how they did it in their themes.

Regards

How can I use AssetBundle in theme? yii2.themefactory theme is showing error while installing. I am using the latest version of Yii framework.

Hi!

Have you actually read:

http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

If yes - what exactly do you not understand?

You can use AssetBundles in themes the same way like everywhere in your app.

Read the guide and take a look into themes from themefactory…

Or take a look in:

app/assets/AppAsset.php

app/views/layouts/main.php

app/web/css/*

Regards