Yii2 widget integrity

The definition and purpose of Yii widget in general is to make universal use. i.e just drop it in the view and its functionality will work.

I have made a custom and simple widget that produce a select list for all jui themes available and by choosing a theme name from the list, the theme used in the jui elements changed.

This theme use two JavaScript files to complete its client-side functionality, which they are linked as follows:

public function run() {


	$this->getView()->registerJsFile('/myjs/jquery.cookie.js', ['depends' => [JqueryAsset::className()]]);

	$this->getView()->registerJsFile('/myjs/JuiThemeSelect.js', ['depends' => [JqueryAsset::className()]]);

	return "$this->label $this->list \n $this->script";


Where those files found in frontend\web\myjs folder.

My question is: How could I maintain this widget to be used in other yii2 projects with those two files? I know that I can, manually, in the new project to create the same file structure and copy those javascripts files to it, but this may leads to missing files, specially, if there is widget with large amount of files. Is there any definition and resources for this concept?

Why not to add it to composer?

you can use it in any project using Composer.

Composer, definitely :)

May I suggest that you take a look at various Yii2 components to see how they set up their composer file.

If you are using code repositories, like git or mercurial, to manage you code, you will be glad to learn that you can get rid of a lot of files because you don’t need to add the ‘vendor’ directory to source control. Only the composer.json file.

I Agree

I think that the solution is Yii2 Extensions, through AssetBundles. By this way, I will able to collect all of my widget’s files (The widget class, css, js) and the MyWidgetNameBundle in one folder and place it in the new project. However, this will need to fix the path of the root folder that I should add the bundle folder to it. For example:

project1\common\MyWidgetName should be placed in common folder of the project2 too. Till this point, I have not implemented what I regarded here yet. Also, I found dealing with composer is too hard and I could not able to figure out it.

I have implement what I regarded in the previous post and packaged it in this file. Just copy the folder named saidbakr in the archive to vendor directory and use the widget.

For more details, please refer to my answer on the question at StackOverFlow from this link