【问题标题】:Render View inside a layout view in yii2在 yii2 的布局视图中渲染视图
【发布时间】:2018-06-02 02:32:40
【问题描述】:

我需要制作一个搜索表单,它将显示在页面顶部的所有页面上,甚至是错误页面上,所以我决定为它制作一个视图和模型,并在主布局中呈现这个视图.所以我创建了一个简单的模型:

SearchModel.php

<?php

namespace app\models;

use Yii;
use yii\base\Model;

class SearchFormModel extends Model
{
    public $query;

    public function rules()
    {
        return [

        ];
    }
}

还有一个观点:

SearchView.php

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'search-nav-form',
    'options' => ['class' => 'form-inline ml-3'],
]) ?>

<div class="input-group input-group-sm">
    <?= $form->field($model, 'text')->textInput(['class' => 'form-control form-control-navbar', 'placeholder'
    => 'Search'])->label('Search'); ?>

    <div class="input-group-append">
        <button class="btn btn-navbar" type="submit">
            <i class="fa fa-search"></i>
        </button>
    </div>
</div>


<?php ActiveForm::end() ?>

这是一个搜索表单。现在我也有了主要布局:

<?php

use app\widgets\Alert;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php $this->registerCsrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

<div class="wrap">


    <div class="container">
        <?= Breadcrumbs::widget([
            'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
        ]) ?>
        <?= Alert::widget() ?>
        <?= $content ?>
    </div>
</div>

<footer class="footer">
    <div class="container">
        <p class="pull-left">&copy; My Company <?= date('Y') ?></p>

        <p class="pull-right"><?= Yii::powered() ?></p>
    </div>
</footer>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

我不明白的是,如何从布局视图中呈现搜索表单?我需要把它放在控制器中,但控制器只为特定页面路由提供操作。

更新

我摆脱了所有错误,但现在表单没有在页面上呈现,甚至没有表单标签。这是我在 ma​​in.php 中呈现它的方式:

 $model = new SearchFormModel;
 $this->render('@app/views/site/SearchFormView',['model' => $model]);

我已经像这样修复了模型 SearchFormModel.php:

class SearchFormModel extends Model
{

    public $search;

    public function rules()
    {
        return [
            // тут определяются правила валидации
        ];
    }
}

【问题讨论】:

  • 你需要echo $this-&gt;render() 而不仅仅是$this-&gt;render() 或者使用短标签,如果它们像这样&lt;?=$this-&gt;render()?&gt;

标签: php yii2 rendering


【解决方案1】:

你可以在布局文件中渲染视图

<?= $this->render('//layouts/path/to/view') ?>

<?= $this->render('@app/views/path/to/view') ?>

或

<?= Yii::$app->view->renderFile('@app/views/path/to/view.php'); ?>

请参阅文档以获取 render() 和 renderFile()

【讨论】:

  • 我试图像这样渲染它:= $this->render('@app/views/SearchFormView') ?> 但我收到错误'view @app/views/SearchFormView需要模型参数'
  • 我也尝试过这样做:$model = new SearchFormModel; $this->render('@app/views/site/SearchFormView',['model' => $model]);这样我得到这样的错误: Unknown Property – yii\base\UnknownPropertyException Getting unknown property: app\models\SearchFormView::text
  • 如果你想将模型传递给searchFormView,那是另一回事,你的错误告诉你SearchFormModel没有text@AdeptusAStrates1字段,你确定这个字段存在于SearchFormModel 或扩展 searchForm 的主要模型?
  • 您正在渲染表单字段&lt;?= $form-&gt;field($model, 'text')-&gt;textInput(['class' =&gt; 'form-control form-control-navbar', 'placeholder' =&gt; 'Search'])-&gt;label('Search'); ?&gt;,这会引发错误
【解决方案2】:

Yii2 中的渲染函数返回渲染的结果。你必须明确地回应。试试吧:

$model = new SearchFormModel;
echo $this->render('@app/views/site/SearchFormView',['model' => $model]);

【讨论】:

  • 这应该是一条评论
【解决方案3】:

您应该将表单提取到小部件中并在布局中使用它。布局应该相对简单,包括一些外部视图看起来像代码味道,在视图文件(包括布局文件)内部创建模型基本上违反了MVP模式。

使用表单创建小部件:

class SearchFormWidget extends \yii\base\Widget {

    public function run() {
        $model = new SearchFormModel();
        $model->load(Yii::$app->request->get());
        return $this->render('search-form-widget', ['model' => $model]);
    }
}

将带有表单的视图放在widgets/views/search-form-widget.php(或类似路径,具体取决于您放置小部件类的位置),并在布局内使用小部件:

<?= SearchFormWidget::widget() ?>

以类似的方式,您可以在任何其他地方使用它。比直接包含视图垃圾更干净和安全的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 2015-12-23
    • 2017-11-10
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2012-05-24
    • 2015-03-08
    相关资源
    最近更新 更多