SlazZe
SlazZe

Reputation: 27

Yii2 location from dependencies in custom asset

I have a path something like /actions/users/someaction in the frontend and I want to use the Bootstrap-Asset (located in /backend/web/assets/xxxxxx/) from the backend.

So I created an asset called "ActionAsset" with following content:

class ActionAsset extends AssetBundle
{
    public $basePath = '@backend';
    public $baseUrl = '@web/backend';
    public $css = [
        'css/external.css',
        'css/overwrite-bootstrap.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BackendBootstrapAsset',
    ];
}

The included css are just working fine, but the dependencies are always saved in /frontend/web/assets/. My question is (and I really searched for weeks) how to change this location to /backend/web/assets.

Upvotes: 2

Views: 2197

Answers (2)

Amit Arya
Amit Arya

Reputation: 142

Asset Publishing As aforementioned, if an asset bundle is located in a directory that is not Web accessible, its assets will be copied to a Web directory when the bundle is being registered with a view. This process is called asset publishing, and is done automatically by the asset manager.

By default, assets are published to the directory @webroot/assets which corresponds to the URL @web/assets. You may customize this location by configuring the basePath and baseUrl properties.

Instead of publishing assets by file copying, you may consider using symbolic links, if your OS and Web server allow. This feature can be enabled by setting linkAssets to be true.

return [
    // ...
    'components' => [
        'assetManager' => [
            'linkAssets' => true,
        ],
    ],
];

With the above configuration, the asset manager will create a symbolic link to the source path of an asset bundle when it is being published. This is faster than file copying and can also ensure that the published assets are always up-to-date.

https://www.yiiframework.com/doc/guide/2.0/en/structure-assets

Upvotes: 0

Nordseebaer
Nordseebaer

Reputation: 138

You need to define $sourcePath. Yii2 AssetManager will copy (or symlink) your assets in your current web/assets/ folder.

From the docs

sourcePath: specifies the root directory that contains the asset files in this bundle. This property should be set if the root directory is not Web accessible. Otherwise, you should set the basePath property and baseUrl, instead. Path aliases can be used here.

So change your code to:

class ActionAsset extends AssetBundle
{
    public $sourcePath = '<path to your asste content>';
    public $css = [
        'css/external.css',
        'css/overwrite-bootstrap.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BackendBootstrapAsset',
    ];
}

Or: Just overwrite your asstetbundle in @frontend/asset and set $sourcePath property accordingly:

class FrontendActionAsset extends ActionAsset
{
    public $sourcePath = '<path to your asste content>'; //you don't need more
}

Upvotes: 1

Related Questions