X
X
xFreake2017-07-06 17:15:55
Yii
xFreake, 2017-07-06 17:15:55

How to pass variable to layouts/main.php?

There is a SiteController function in it

public function index(){
    $contactForm = new FooterForm();
    if ($contactForm->load(Yii::$app->request->post()) && $contactForm->validate()) {
        if ($contactForm->sendEmail()) {
            Yii::$app->session->setFlash('success', 'Спасибо - мы свяжемся с Вами в ближайшее время');
        } else {
            Yii::$app->session->setFlash('error', 'Произошла ошибка при отправке сообщения.');
        }

        return $this->refresh();
    }
    return [
        'contactForm' => $contactForm,
    ];
}

If in main.php var_dump($contactForm); then you get the error
Undefined variable: contactForm
public_html/plitka/engine/frontend/themes/magichotel/views/layouts/main.php 176

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Kryanga, 2017-07-06
@afc59

I think $contactForm is a local variable that will be destroyed when the function exits. To pass a reference to new FooterForm() assign it to a global variable

D
Dmitry, 2017-07-06
@slo_nik

Good evening.
In the controller, create a public property, such as $test.
In the controller action, you assign a value to it.
In layouts/main.php you refer to it like this
$this->context->test

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question