Search code examples
phpcssfontsyii2assets

Yii2 BaseUrl for bootstrap fonts


I need your help guys. I changed directory for all Assets. It works properly, but not for bootstrap fonts.

For Ex: path for css and js file now is project/www/web_assets/all.css and Yii2 found them properly.

But it does not working for fonts. Yii2 is looking for fonts at wrong derictrory /var/www/tt_yii/web/assets/582582f3/fonts/glyphicons-halflings-regular.woff2.


Solution

  • Bootstrap gets used at different places. It is already configured by default by the BootstrapAsset and is a dependency for other pre-defined assets. But you can override the default location.

    Assuming you have the same file structure in web_assets as it can be found in 'vendor/bower-asset/bootstrap/dist' (contains folder css, fonts and js) you can add the following to your components configuration:

    use yii\bootstrap\BootstrapAsset;
    
    ...
    
    'components'          => [
       ...
        'assetManager' => [
            'bundles' => [
                BootstrapAsset::class => [
                    'sourcePath' => null,
                    'baseUrl'    => '@web/web_assets',
                ],
            ]
        ],
        ...
    ],
    

    @web points to the web folder where the index.php and your web_assets directory file should exist as well. sourcePath gets set to null since baseUrl wouldn't be evaluated.

    Further information about configuring the used bundles can be found at AssetManager::$bundles or in the guide.

    Similar question is here.