Answer the question
In order to leave comments, you need to log in
YII2 getting data from db in widget?
Hello.
To output and display data from the database in layouts main.php, as I understand it, you need to use widget.
Here is the simplest example of creating a widget:
New Widget
namespace app\components;
use yii\base\Widget;
use yii\helpers\Html;
class NewsWidget extends Widget
{
public $message;
public function init(){
parent::init();
if ($this->message === null){
$this->message = 'News today';
}
}
public function run(){
return Html::encode($this->message);
}
}
NewsWidget::widget(['message' => 'Good day']);
Answer the question
In order to leave comments, you need to log in
Something strange... if I were you, I would do it a little differently... I would display cached news in the layout.
IMHO if the News is constantly on all pages .. IMHO it is better to cache and display, especially since this is not very critical information ..
Use models. Widgets can also render view.
...
public function run()
{
$model = News::find() -> limit(10) -> all();
return $this -> render('news', ['news' => $model);
}
...
<?php echo NewsWidget::widget(); ?>
In widgets, it is better not to receive data at all, neither through models, nor through direct access to the database. Pass pure data there, as is done with breadcrumbs, for example, or at least some DataProvider that also hides the real data source.
I would override the yii\web\View class, add a news field to it to store a list of news, and explicitly pass it to the widget in the template. The data itself in the View can be customized in the controller, or if this data is needed everywhere, it can be done in the View class itself.
Something like this:
<?php
class NewsProvider
{
public function getNews()
{
return News::find()->all();
}
}
class View extends yii\web\View
{
/**
* @var NewsProvider
*/
private $newsProvider;
/**
* @var array
*/
private $news;
public function __construct(NewsProvider $newsProvider, array $config = [])
{
$this->newsProvider = $newsProvider;
parent::__construct($config);
}
public function getNews()
{
if (!$this->news) {
$this->news = $this->newsProvider->getNews();
}
return $this->news;
}
}
echo NewsWidget::widget(['items' => $this->getNews()]);
// в файле config/web.php
'components' => [
'view' => 'app\components\web\View',
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question