我有基本的Yii2,并希望从本课https://github.com/yiisoft/yii2/blob/master/docs/guide/start-databases.md显示页面(没有分页)
Controller return:
return $this->render('index', [
'countries' => $countries,
'pagination' => $pagination,
]);
查看文件:
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Countries</h1>
<ul>
<?php foreach ($countries as $country): ?>
<li>
<?= Html::encode("{$country->name} ({$country->code})") ?>:
<?= $country->population ?>
</li>
<?php endforeach; ?>
</ul>
也许我应该更改主布局以显示JSON,在哪里以及如何正确地执行它?
答案 0 :(得分:0)
在Yii2中,你需要从你的行动中返回结果,例如JSON:
public function actionIndex()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$query = Country::find();
$countries = $query->orderBy('name')->all();
return $countries;
}