如何在视图文件中显示数据

时间:2017-05-21 06:13:06

标签: php yii2

我正在尝试从会话中获取用户的数据(first_name,last_name,phone,email)。

public function actionIndex(){
   $query = User::findOne(Yii::$app->session['user']['id']);

  return $this->render('index', ['query' => $query]);
}

我尝试在视图文件中使用DetailView :: Widget,但它没有削减任何冰。 有人可以帮我如何在列表中的视图文件中显示这些数据吗?

Viewfile:

    echo DetailView::widget([
    'model' => $model,
    'attributes' => [
        'title',               // title attribute (in plain text)
        'description:html',    // description attribute in HTML
        [                      // the owner name of the model
            'label' => 'Owner',
            'value' => $model->user->last_name,
        ],
        //'created_at:datetime', // creation date formatted as datetime
    ],
]);

1 个答案:

答案 0 :(得分:0)

$this->render('index', ['model' => $query]); } 

将密钥**query**更改为model。或者在视图中将$model更改为$query

如果用户已登录,并且您使用app / model / user model,则可以使用:

$user = \Yii::$app->user->identity;
echo $user->last_name;
echo $user->id;

Viewfile:

    echo DetailView::widget([
    'model' => $model,
    'attributes' => [
        'title',               // title attribute (in plain text)
        'description:html',    // description attribute in HTML
        [                      // the owner name of the model
            'label' => 'Owner',
            'value' => \Yii::$app->user->identity->last_name,
        ],
        //'created_at:datetime', // creation date formatted as datetime
    ],
]);