What's with all the code in the view?

I’m moving from Yii 1.1 and I am used to keeping the view files clean of code as much as possible for our front-end developers. They are also used to working in Smarty, so that’s where I’m going with this.

How would I load the following in a controller action?


use yii\helpers\Html;

use yii\bootstrap\Nav;

use yii\bootstrap\NavBar;

use yii\widgets\Breadcrumbs;

use app\assets\AppAsset;

/* @var $this \yii\web\View */

/* @var $content string */


$this->title = 'Contact';

$this->params['breadcrumbs'][] = $this->title;


Also, can someone explain what the purpose of these?

<?php $this->beginPage() ?>

<?php $this->beginBody() ?>

<?php $this->endBody() ?>

<?php $this->endPage() ?>

Thirdly, is there an easy way to make things available in the view from the controller? I’m used to just making getters in the controller, but now $this is the view instead…




Read "Layouts" here:


"Most layouts should call the following methods like shown in the above code. These methods mainly trigger events about the rendering process so that scripts and tags registered in other places can be properly injected into the places where these methods are called."