I'm trying to write my own widget that I would like to put in the vendor/sergalas/header folder

Here is the code:

 <?php namespace sergalas\header; use yii\base\Widget; use common\models\Category; use yii\helpers\ArrayHelper; class Menus extends Widget { public $menu; public function init() { parent::init(); $cat=Category::find()->where('tags=0')->all(); $this->menu=ArrayHelper::toArray($cat,[ 'common\models\Category' => [ 'id', 'name_category', ], ]); } public function run() { return 'Hello'; } } 

Please explain where and what is the error and how to fix it. Or at least where to find the answer?

  • The vendor folder should not be touched at all. She's managed by Composer. Check out the documentation and follow the example. - Roman Grinyov
  • And the file in which you place the class, what is the name? - cheops

1 answer 1

The vendor folder belongs to the composer and is fully managed by it. In order for your code to appear in this folder, you need to add your package to packagist.org. This is done by adding the file composer.json to the root of your project on the github.

In yii2, widgets are usually located in the components folder, at least this is what the documentation advises.