【问题标题】:Yii2 - Render form inside footer (main.php)Yii2 - 在页脚内渲染表单 (main.php)
【发布时间】:2015-04-01 17:14:34
【问题描述】:

在 Yii2 的 main.php 布局文件中,我需要渲染一个位于文件夹 contacto/_form 中的表单。

我如何将 $model 变量传递给文件 main.php,在 layouts 文件夹中,并在以下位置使用它:

<?= $this->render('_form', [
    'model' => $model,
]) ?>

非常感谢。

【问题讨论】:

    标签: php forms model yii2


    【解决方案1】:

    您可以这样创建小部件:

    class FormWidget extends Widget
    {
        /**
         * @return string
         */
        public function run()
        {
            $model = ...;// code to create model
    
            return $this->render('_form', [
                'model' => $model
            ]);
        }
    }
    

    layout 这样的输入小部件中:

    <?= FormWidget::widget() ?>
    

    有关创建小部件的更多信息 - http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html#creating-widgets

    【讨论】:

    • 非常感谢,但它总是抛出错误:找不到类“小部件”。我将类 FormWidget extends Widget 放在位于根/组件的小部件中。有什么想法吗?
    • 好的。它现在正在工作。非常感谢您指出正确的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2021-03-25
    相关资源
    最近更新 更多