在视图yii2

时间:2017-03-21 08:06:06

标签: php yii2

我试图在actionView方法中渲染两个模型,但没有结果。检查了以前问题的一些答案,似乎我的方法没问题,但仍然无效。

这是方法视图的内容:

public function actionView($id)
    {
        $postModel = Post::find()->where(['post_id' => $id])->one();

        $currUser = BlogUser::find()->where(['id' => $postModel->user_id])->one();

        return $this->render('view', [
            'model' => $this->findModel($id),
            'author' => $currUser
        ]);
    }

和我的观看页面:

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Post */

//$this->title = $model->title;
//$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];
//$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-view">

    <div class="content">
        <div class="row">
            <div class="col-md-3">

               <?= $model->content;
                   $author->username;
               ?>

            </div>
        </div>
    </div>

</div>

它告诉作者变量未定义,但是当我在动作中渲染它时无法实现。将欣赏每一个建议!谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用:

<div class="post-view">

    <div class="content">
        <div class="row">
            <div class="col-md-3">
              <?= $model->content; ?>
             <?= $author->username; ?>
          </div>
        </div>
    </div>

</div>