Finally, I found a solution. It depends on the Yii2 and AssetBundles extensions . The story is simple, just make all the files in one folder, placed in one of the Yii2 default folders, for example: common, vendor. - By the way, the provider is found in both the base and the advanced application template yii2.
In addition to all the files, that is, for my case, the widget php file and javascripts files, you need to create a php class file YourWidgetNameAsset. Indeed, the main key to the solution lies in this class.
My case
I have a JuiThemeSelectWidget widget that I placed in a folder called saidbakr in the vendor directory, so we have a vendor\saidbakr . This folder contains the following four files:
- JuiThemeSelectWidget.php
- JuiThemeSelectAsset.php
- JuiThemeSelect.js
- jquery.cookie.js
File number 3 depends on file 4 for creating cookies in order to save the user's last choice.
Now we see the code of file 2 JuiThemeSelectAsset.php :
<?php namespace vendor\saidbakr; use yii\web\AssetBundle; class JuiThemeSelectAsset extends AssetBundle { public $sourcePath = '@vendor/saidbakr'; public $autoGenerate = true; public $js = ['jquery.cookie.js','JuiThemeSelect.js']; public $depends = [ 'yii\jui\JuiAsset', ]; }
Here we defined an AssetBundle for a widget similar to that described in this official source .
Now we look at the title of the widget class itself and its run() method:
<?php namespace vendor\saidbakr; use yii; use yii\base\Widget;
Itβs clear that we used the asset package as described in this link , but here we used $this->getView() instead of $this , because the method is not called from the view.
I compressed the folder named saidbakr and uploaded it to this location or pulled out this GitHub Repository to check what I did, whose name is Yii2 Extension . Just extract the contents of the archive into a folder named saidbakr right below the provider folder. Thus, the file structure should be "vendor \ saidbakr" (the four files discussed in the list above), and use the widget in your views something like this:
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\jui\DatePicker; use vendor\saidbakr\JuiThemeSelectWidget; ?> <div> <?= JuiThemeSelectWidget::widget(['label' => 'Select New JUI Theme', 'themeListId' => 'fox']) ;?> <div class="profile-form"> </div> <h2>Testing Elements for the JUI</h2> <form> <select id="sel"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> </form> <?php $this->registerJs("$('#sel').selectmenu();") ;?>