How to publish an asset to a widget in Yii2

In Yii 1, you could publish an asset using:

Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.MyWidget.assets'));

How to publish an asset to a widget in Yii2?

+4
source share
6 answers

Thank you for your help, this was the final code to solve the problem:

list($path, $webPath) = Yii::$app->getAssetManager()->publish(__DIR__."/assets);
$this->getView()->registerJsFile($webPath);
+2
source

In the field of view of your widget:

app\assets\AppAsset::register($this); // $this == the View object

Mark the documents .

+3
source

:

app\assets\AppAsset::register($this->getView());

.

+2
+1

.

Yii2 AssetBandle , -. Yii1 . .

:

(yii\web\AssetBundle) , , . . :

YourAsset::register($view); 

 $view->registerAssetBundle(InterrogateDeviceButtonAsset::class);
0

BaseController.php, . .

/**
 * Publish an asset and return url
 *
 * @param $src
 *
 * @return mixed
 */
public function publishAsset( $src ) {
    $path = Yii::getAlias( $src );
    if ( ! $this->assetManager ) {
        $this->assetManager = new AssetManager();
    }
    $return = $this->assetManager->publish( $path );
    return $return[1];
}

The only thing I'm trying to configure is to enable the full URL scheme. I posted my question that here the Yii2 AssetManager Published Path includes a URL scheme

-1
source

Source: https://habr.com/ru/post/1570190/


All Articles