V
V
vitaly_742019-03-11 15:48:26
Yii
vitaly_74, 2019-03-11 15:48:26

Why does Yii2 not see the widget, and how can I fix it?

Good afternoon, I'm learning to deal with widgets in the advanced version. I want to keep all my widgets in the frontend/widgets folder (for me it's more convenient and seems more logical). So I created a widget with my views 5c86582c1e973699037425.png
in the controller, I wrote:

<?php
namespace app\MyWidget;
use yii\base\Widget;
class breadcrumb extends Widget {
    public function run(){
        return "hello World";
    }
}

written in the template
use app\MyWidget;
MyWidget\breadcrumb::widget();

and an error occurs saying that yii2 cannot find this class
Class 'app\MyWidget\breadcrumb' not found

The question is how to correctly connect widgets from this folder?
and how to connect the widget view correctly?
please do not write that they say it is not practical, or it is not right to scatter files in this way (it is not practical for you, it is convenient for me). thank.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-03-11
@vitaly_74

where did app come from if it's advanced and not basic? Where did MyWidget come from? Actually, the error tells you that it does not understand where 'app\MyWidget' came from and what you decided that it should be.
As I understand it, instead of it, namespace app\MyWidget;I should namespace frontend/widgets/breadcrumb;
also not quite understand what the controller and model do in your widget? Are you sure you are not confusing a widget and a module?
And a very big request, class names with a capital letter. See how the yii developers worked hard pressing shift until the pain in the little finger (Widget), and you are lazy (breadcrumb)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question