Rugleh Rugleh - 8 months ago 54
PHP Question

how to render a view from controller1 into a view of controller2 yii2?

I'm new to yii2 and looking for a little of help.

This is part of my

in the site controller (basic template yii2

<?= $this->render('//tours/_form.php ') ?>

And I need to render the
(which is a view of tours controller) in this

but the error is this:

Undefined variable: model

I think the problem is in the siteController
but what should I add to it?

I understand how to render views that have the same controller, but I'm assuming that this maybe is different.

Thanks in advance for your help


This my controller action from the site controller, this is just as gii generated it

public function actionIndex()
return $this->render('index');

maybe there I have to call the tour model?

Answer Source

from your index action you are not returning any model, you must return the model which you wan to access on your view page,

Note you can return multiple models also

public function actionIndex()
        $model  = User::find()->where(['name' => 'CeBe'])->one(); // dummy example

        return $this->render('index', ['model'=>$model]); // this can be used on your index page

model returned from your index action will be accessible on your view page Please refer Yii2 Controller guide to know more about action