2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
SlazZe
  • 27
  • 4

2 Answers2

1

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
}
Nordseebaer
  • 138
  • 10
  • well, I already tried this. If I take the path like $sourcePath = '@backend/web' nothing will be saved in /backend/web - it will still saved in frontend/web. But also my normal css will saved in frontend/web/assets/xxxxxx and I don't want that. – SlazZe May 16 '17 at 15:51
  • To clarify: When you run your backend application, only the content `backend/web/*` is or should be readable. If you start your frontend application, all assets needed by your app must be copied to `frontend/web/assets`. You shouldn't access your backend assets in `backend/web/assets` from frontend (dunno if its possible). If you don't want to duplicate your content edit your config: `'components' => ['assetManager' => ['linkAssets' => true]]` or set the `publishOptions` in your AssetClass to `[linkAssets' => true]`. The AssetManager will symlink your assets. Try to empty your asset folders. – Nordseebaer May 18 '17 at 20:40
  • Thank you, that helped me alot. – SlazZe May 20 '17 at 11:13
0

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

Amit Arya
  • 142
  • 2
  • 13