Widget in Yii2

Hi

How to create Widget in advanced template of Yii2?

Please explain with example?

http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html#creating-widgets

Hi

I used from advanced template of Yii2 and [size="2"]created the file of [color="#ff0000"]HelloWidget [/color]in address of [color="#ff0000"]advanced\common\components[/color] and code of this file is in the below:[/size]


<?php

namespace common\components;

 

use yii\base\Widget;

use yii\helpers\Html;




class HelloWidget extends Widget

{

    public $message;

 

    public function init()

    {

        parent::init();

        if ($this->message === null) {

            $this->message = 'Hello World';

        }

    }

 

    public function run()

    {

        return Html::encode($this->message);

    }

}



Then I went to index.php in site view and imported instruction of [color="#ff0000"]use common\components\HelloWidget;[/color] in the top of page and used from widget to below shape:


<div class="col-lg-3" style="background-color:blue;height:500px;">

        <?= HelloWidget::widget(['message' => 'Good morning']) ?>

    </div>

and when the browser is opened,the below error is showed to me?

[size="4"][color="#ff0000"][font="inherit"]PHP Fatal Error[/font] – yii\base\ErrorException[/color][/size]

[color="#FF0000"][size=“2”]Class ‘common\components\HelloWidget’ not found[/size][/color]

could be case sensitivity issue… how did you name your file?

Btw… when posting code please use the code tag ("<>" on the menu editor)

My file name is[color="#ff0000"] HelloWidget .php[/color],please help me?

There is the Alert widget in common/widgets folder. Does the same happen when you use it in your view?

Place your widget in the same folder with the proper namespace and try it from there.

when put my widget in address of advanced\frontend\widgets and why use from this in your view with example?

The problem should be in the filename… you wrote above "HelloWidget .php" is there a space or was a typing mistake?

I’m sorry mate. I would like to help but I don’t understand what you said.

I’m telling you to create a widget to be in the same folder as Alert widget which is already implemented for advanced template and then to use it in your view file.

Please explain with example?

Hi,

How about answering questions like this?

Have you checked this already?

Regards

Your example is inside common/widgets folder, the file called Alert.php.

My problem is solved.

Thansk from you,[size=2]I hope successful in your life.[/size]

At least write what was the problem and how you fixed it.

[size=2]How to my top problem is solved:[/size]

[size=2]I replaced[/size][size=2] [/size][color=#FF0000][size=2]HelloWidget .php [/size][/color][size=2] to [/size][size=2] [/size][color=#FF0000][size=2]HelloWidget.php [/size][/color][size=2] [/size][color=#FF0000][size=2] .[/size][/color]