Passing Data from Controller to View

Render the data passed to view from the controller

We are familiar with the view template and the layout (main.php) in the Yii 2 Advanced Framework. For now, we'll just focus on passing the data from the controller to the view and rendering it on the browser. In next articles, I'll cover the layout in depth.

Yii 2 is an MVC Framework

In an MVC Framework, M - Model, V - View and C - Controller. The bifurcation of the functionalities is done in such a manner that Models hold the business logic, Views display the data to the user via templates and Controllers accept the data from the views and send it to model or vice versa.

Let's start with the coding. Open the advanced/frontend/controllers/SiteController.php file. We have already worked on this file where we set the template to false resulting the output on the browser to be plain HTML code without CSS, JS, header and footer. Now define and initialize a variable in the actionIndex() function and pass it in an array as the second parameter to the render() function. Take a look at the following code.


public function actionIndex()
{
    $h1 = 'This is SPARTA!';
    return $this->render('index', ['h1' => $h1]);
}

What's happening? We have declared a variable $h1 which is passed or to be specific, injected into the view template. This variable will have the name $h1 in the view too i.e. whatever we define as the key. In order to change the name of the variable to $title use the following code.


return $this->render('index', ['title' => $h1]);

How to use this in the view? Below is the complete code of advanced/frontend/views/site/index.php file. Here, we can see that we have <?= $title ?> on the fourth line inside h1 tag. This is where the variable $title will be printed and displayed in the browser.


<div class="site-index">

<div class="jumbotron">
<h1><?= $title ?></h1>
</div>

<div style="text-align: center;">
Your ultimate guide for Yii 2 development
</div>
</div>

As you can see from the snap, the output is as expected. We can inject multiple variables into the view or just a single array with multiple elements. Also, we can pass in the objects of class into the view. There is no restriction of what kind of data and how much data you pass to the views from the controller.

Passing Data from Controller to View

Let's write some more code! We'll now pass two variables to the controller and use those two variables to display the values on the browser.


/* advanced/frontend/controllers/SiteController.php */
public function actionIndex()
{
$h1 = 'This is SPARTA!';
$h4 = 'Codegear is your ultimate guide for Yii 2 development';
return $this->render('index', ['title' => $h1, 'subTitle' => $h4]);
}

 


<!-- advanced/frontend/views/site/index.php  -->
<div class="site-index">

<div class="jumbotron">
<h1><?= $title ?></h1>
</div>

<div style="text-align: center;">
<?= $subTitle ?>
</div>
</div>

The output is below.

Passing Multiple Variables from Controller to View

Now we'll pass the DateTime object to the view. This is just to demonstrate the passing the objects to the view and manipulating it in the view as per the requirements of UI/UX.


/* advanced/frontend/controllers/SiteController.php */
public function actionIndex()
{
$h1 = 'This is SPARTA!';
$h4 = 'Codegear is your ultimate guide for Yii 2 development';
$dateTime = new \DateTime();

return $this->render('index', ['title' => $h1, 'subTitle' => $h4, 'dateTime' => $dateTime]);
}

Now, we'll format the object to display day and time on the page, in the view. So every time the page is refreshed, we can see the server's date and time. As PHP is compiled at the server side the DateTime object has the date and time values as per the server. If the timezone in your php.ini file does not have any value set, then it will use the UTC timezone.

php.ini - timezone

timezone is the default value used by all date/time functions. In case the value is not set, PHP uses UTC timezone. You can set it to your local timezone but make sure all of your data/time manipulations are in sync with the timezone.

Passing Object from Controller to View

Awesome! We injected the DateTime object in the view and have used it to display the date and time in the format as per our requirements. Comment in the below section for any doubts, views or suggestions.