Answer the question
In order to leave comments, you need to log in
Why is the value not being passed?
I understand that I do not understand anything. Therefore, I asked the question of how to transfer data from the database to the template (layouts-> main) I was advised through the widget. From all the documentation about widgets, I found only how to create a HelloWidget, I tried it myself, here is the widget code:
<?php
namespace common\widgets;
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 $this->menu;
}
}
use common\widgets\Menus;
...
<?=Menus::widget(); ?>
Answer the question
In order to leave comments, you need to log in
Menu::widget() will output what the run() function returns on the given widget.
Those. you need to return not an array, but a string with a list of links.
You can make a view for the widget and in run() do return $this->render('menu'). In views/menu.php you will have your menu template. The views folder must be in the same place as the widget itself, otherwise the full path must be passed to render.
Cramming all the logic into a widget is a very quick and easy solution, but a bad one. You can imagine that you have a small black hole in your view that receives information from somewhere. Widgets are needed only to encapsulate the output logic, and not to receive and process data.
The question arises, how to pass data from the model / controller to the layout. Answer: by overriding the renderContent method . If there are many controllers, create a base class with renderContent implemented and inherit from it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question