dqqt31923 2016-11-24 22:50
浏览 69
已采纳

如何将一个视图从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

Edit:

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?

  • 写回答

2条回答 默认 最新

  • duanliao6789 2016-11-24 23:25
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部