2016-11-25 42 views
0

我是yii2的新手,並尋求一點幫助。如何將controller1的視圖渲染到controller2的視圖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'); 
    } 

也許有我有打電話給旅遊模式?

+0

顯示您的控制器行動 –

+0

@NitinP添加到帖子 – Rugleh

+0

是的,你需要呼叫旅遊模型,並將該模型實例傳遞到您的瀏覽頁面 –

回答

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以瞭解更多關於行動

+0

非常感謝您的幫助!現在它的作品:D – Rugleh

+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]); 
} 

但是你必須先設置這個變量。

+0

謝謝!我添加了 render('// tours/_form.php',['model'=> $ model])?>但仍然得到相同的錯誤未定義的變量:model – Rugleh

+0

是的,你是對的,我錯過了模型現場控制器的行動,感謝您的幫助! – Rugleh

+0

請將答案標記爲已接受。 – Bizley