There are two widgets (example from docks)

The first

namespace app\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); } } 

And the second

 namespace app\components; use yii\base\Widget; use yii\helpers\Html; class WorldWidget extends Widget { public function init() { parent::init(); } public function run() { $this->message // получить переменную виджета HelloWidget } } 

Widgets are called one after another in one view, i.e.

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

Is it possible to get the value of the $ message variable of the HelloWidget widget in the WorldWidget widget? Try so

 \Yii::$app->view->message 

I get

Getting unknown property: yii \ web \ View :: message

1 answer 1

I believe that within the framework of this question, the essence of a more complex task is simply schematically described.

It is not good to transfer anything from one widget to another. Since widgets, following ideology, this is a separate program unit.

But if such a task arose, then I would solve it this way:

 Hello Widget public function run() { \Yii::$app->params['some_value'] => $this->message; return Html::encode($this->message); } WorldWidget $value = \Yii::$app->params['some_value']; 

Perhaps this is not the best option, but as part of the originality of the task (apparently this is a very important parameter that needs to be passed), it may be appropriate.