Using the controller context in a view
Yii views are pretty powerful and have many features. One of them is that you can use the controller context in a view. So, let's try it.
Getting ready
Create a new application using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
How to do it…
Create a
controllers/ViewController.php
as follows:<?php namespace app\controllers; use yii\web\Controller; class ViewController extends Controller { public $pageTitle; public function actionIndex() { $this->pageTitle = 'Controller context test'; return $this->render('index'); } public function hello() { if (!empty($_GET['name'])) { echo 'Hello, ' . $_GET['name'] . '!'; } } }
Now, we will create a
views/view.php
showing what we can do:<h1><?= $this->context->pageTitle ?></h1> <p>Hello call. <?php $this->context...