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

时间:2016-11-25 06:50:31

标签: php yii yii2

我是yii2的新手并且正在寻求一些帮助。

这是我在网站控制器index.php中的一部分(基本模板yii2 views/site/index.php

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

我需要在tours/_form.php

中渲染index.php(这是巡视控制器的视图)

但错误是这样的:

  

未定义的变量:模型

我认为问题出在siteController中 但我应该添加什么呢?

我理解如何渲染具有相同控制器的视图,但我假设这可能不同。

提前感谢您的帮助

编辑:

我的控制器操作来自站点控制器,这就像gii生成它一样

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

也许在那里我必须打电话给旅行模型?

2 个答案:

答案 0 :(得分:0)

很可能你没有将$model变量传递给调用它的局部视图。这样做:

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

当然,为了实现这一点,必须有一个$model变量才能通过。它通常以相同的方式从控制器传递到视图。

public function actionIndex()
{
    $model = 1; // init the variable

    return $this->render('index', ['model' => $model]);
}

但你必须先设置这个变量。

答案 1 :(得分:0)

从你的索引操作中你没有返回任何模型,你必须返回你想在视图页面上访问的模型,

注意您也可以返回多个模型

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
    }
您可以在视图页面上访问从索引操作返回的

模型 请参阅Yii2 Controller guide了解有关行动的更多信息

相关问题