I
I
Ilya Beloborodov2016-12-06 15:51:35
Yii
Ilya Beloborodov, 2016-12-06 15:51:35

How to properly create widgets in Yii2?

There is a widget.

- components
     - widgets
          - NewsChat
               - assert
               - NewsChatAutloload.php

File NewsChatAutloload.php
<?php
namespace app\components\widgets;
class NewsChatAutoload extends \yii\base\Widget {

    public function init() {
        parent::init();
    }
    public function run() {
        echo 1;
    }
}

when calling the widget
<?=app\components\widgets\NewsChatAutoload::widget()?>

throws an error
Class 'app\components\widgets\NewsChatAutoload' not found
What's wrong with the paths?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-12-06
@kowap

you have a problem with the namespace
namespace must match the directory structure, and you have a namespace for the app\components\widgets widget, and the widget itself is located in the app\components\widgets\NewsChat directory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question