I
I
Igor Vasiliev2017-03-24 14:13:21
Yii
Igor Vasiliev, 2017-03-24 14:13:21

Yii2 how to pass data from db to layout once?

Hello.
--
There is a solution that allows you to display content in a layout:

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

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

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

This means that if I forget to specify this construction in the controller , or rather in a specific action , then the site on this page will simply collapse, I would like to know how to display this once in the layout, without repeating in actions, and without displaying a direct request to bd, by type:
$x = Site::find()->where(['public' => $act])->all();

If there is no other way, just write like this, short and clear. Thank you for your tolerance for the issue and respect for the desire to code correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-03-24
@Isolution666

Create a widget that performs the functionality you need and display it in a layer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question