<?php
/* @var $this \yii\web\View */
/* @var $content string */
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 name = "viewport" content = "width = device-width, initial-scale = 1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class = "wrap">
<?php
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
[
'label' => 'Logout (' . Yii::$app->user->identity->username.')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
]);
NavBar::end();
?>
<div class = "container">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this>params
['breadcrumbs'] : [],
]) ?>
<?= $content ?>
</div>
</div>
<footer class = "footer">
<div class = "container">
<p class = "pull-left">© My Company <?= date('Y') ?></p>
<p class = "pull-right"><?= Yii::powered() ?></p>
</div>
</footer>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
这种布局是所有视图的通用页面生成HTML页面。$content 变量是视图渲染内容的结果。以下方法引发有关渲染过程事件,以便在其他地方注册的脚本和标签会被适当注入 -
-
head() − 应在头部分调用。产生一个占位符,将与定位于头部位置已注册的 HTML 来代替。
-
beginBody() − 应在 body 部分的开头调用。触发 EVENT_BEGIN_BODY 事件。产生将使用定位在 body 已注册的HTML,将替换占位符开始位置。
-
endBody() − 应在 body 结束部分被调用。触发 EVENT_END_BODY 事件。
产生一个占位符,这将有针对性的在 body 的结束位置使用已注册的HTML来代替。
-
beginPage() − 应在布局的开头被调用。触发EVENT_BEGIN_PAGE 事件。
-
endPage() − 应在布局结束时调用。触发 EVENT_END_PAGE 事件。
创建布局
<?php
/* @var $this \yii\web\View */
/* @var $content string */
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 name = "viewport" content = "width = device-width, initial-scale = 1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class = "wrap">
<div class = "container">
<?= $content ?>
</div>
</div>
<footer class = "footer">
<div class = "container">
<p class = "pull-left">© My Company <?= date('Y') ?></p>
<p class = "pull-right"><?= Yii::powered() ?></p>
</div>
</footer>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller {
public $layout = "newlayout";
/* other methods */
}
?>

<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
$this->title = '关于我们';
$this->params['breadcrumbs'][] = $this->title;
$this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, Yii教程, Yii视图,
meta, 标签']);
$this->registerMetaTag(['name' => 'description', 'content' => '这是一个页面的描述!'], 'description');
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>
This is the About page. You may modify the following file to customize its content:
</p>
<code><?= __FILE__ ?></code>
</div>
我们刚刚注册了两个 meta 标签 − keywords 和 description.
第6步 - 现在打开 http://localhost:8080/index.php?r=site/about ,你会发现在页面头部的 meta 标签内容如下面的屏幕截图。
-
EVENT_BEGIN_BODY − 触发了布局通过调用 yii\web\View::beginBody()
-
EVENT_END_BODY − 触发了布局通过调用 yii\web\View::endBody()
-
EVENT_BEGIN_PAGE − 触发了布局通过调用 fyii\web\View::beginPage()
-
EVENT_END_PAGE − 触发了布局通过调用 yii\web\View::endPage()
-
EVENT_BEFORE_RENDER − 在控制器开始渲染一个文件时触发
-
EVENT_AFTER_RENDER − 渲染文件后触发
public function actionAbout() {
Yii::$app->view->on(yii\web\View::EVENT_BEGIN_BODY, function () {
echo date('Y-m-d H:i:s');
});
return $this->render('about');
}
要点
- 将复杂的视图分成几个小块文件
- 使用公用的HTML代码部分(页眉,页脚,菜单等)的布局
- 使用小部件-widgets
- 包含HTML和简单的PHP代码格式化和呈现数据。
- 无处理请求
- 不修改模型属性
-
不执行数据库查询

