D
D
darknet372017-03-18 11:08:19
Yii
darknet37, 2017-03-18 11:08:19

How to display data from the database in Yii2 layouts?

I need to display data from the database to the header of the site - this is the template layouts-> main.php
Where and how to get data from the database and how exactly to transfer it to main?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
myspace, 2017-03-18
@darknet37

Via widget or app settings

D
Dmitry, 2017-03-18
@slo_nik

Good morning.
Data can be obtained in the model, it can be in the controller.
To pass data to the template, do the following.
In the desired controller, create a public variable, let's call it test. In the same controller, give it a value and call it in the template.

// в контроллере
public $test

public function actionIndex(){
  $this->test = // присвоить значение, или результат работы метода
}

// в шаблоне 
        if(isset($this->context->test) && $this->context->test != null)
        {
            echo $this->context->test;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question