2016-11-25 06:50

如何将一个视图从controller1渲染到controller2 yii2的视图?


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

This is part of my index.php in the site controller (basic template yii2 views/site/index.php)

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

And I need to render the tours/_form.php (which is a view of tours controller) in this index.php

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanliao6789 duanliao6789 5年前

    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

    点赞 评论 复制链接分享
  • douqianrou9079 douqianrou9079 5年前

    Most likely you have not passed $model variable to this partial view which is calling it. Do it like that:

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

    Of course for this to work there must be a $model variable to pass. It usually is passed from the controller to the view in the same manner.

    public function actionIndex()
        $model = 1; // init the variable
        return $this->render('index', ['model' => $model]);

    But you have to set this variable first.

    点赞 评论 复制链接分享