dqqt31923 2016-11-25 06: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-25 07: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条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)