robsch
robsch

Reputation: 9728

Yii2 extension: How to declare $sourcePath in assets?

I'm developing an Yii2 extension that uses an AssetBundle for some js/css files:

namespace myExtension/assets;

class ResourcesAsset extends AssetBundle {
    public $sourcePath = '@app/assets/resources';
    public $css        = ['resources.css'];
    public $js         = ['resources.js'];
    public $depends    = ['yii\bootstrap\BootstrapPluginAsset'];
}

Structure in extension:

my-extension
|-assets
| |-resources
|   |-resources.css
|   |-resources.js
|   ResourceAsset.php
|-controllers
|-models
|-views
|-Module.php

This doesn't seem to work when I install the extension in another project because of the $sourcePath. There the alias @app points to the application base path, not to the extension root folder in vendor.

Currently, I use @myExtension/assets/resources, which works as I know it gets that alias in extionsions.php when it gets installed. But I think this is not optimal. So what is the optimal way to declare $sourcePath? Should I create an own alias (in Module.php?)? Should I use __DIR__?

Upvotes: 0

Views: 1334

Answers (1)

Bizley
Bizley

Reputation: 18021

The alias you are using (@myExtension) is the way to go.

From the Guide:

When the extension is installed in an application, Yii will create for each listed root namespace an alias that refers to the directory corresponding to the namespace.

Alternatively you can use alias for vendor folder so something like:

public $sourcePath = '@vendor/my-extension/assets/resources';

Upvotes: 1

Related Questions