Yii Actions and Views Tutorial

Yii Actions and Views Tutorial

Now that we have Yii Installed, we need to start kicking the tires so to speak. Since Yii is a Model View Controller framework, it uses the familiar controller paradigm to handle incoming requests, decides how to work with that request, then renders a view. Most frameworks follow this basic principle, yet they all implement it in slightly different ways. Yii’s implementation is really nice. We’re starting at the very basics here just to see if we can get a few things working. Let’s test it out.

Let’s go ahead and create a new page in our application. We’ll make use of an action and a view to do this. Actions in Yii are the methods of the controller class. We’ll create an action that displays a stock chart based on the stock ticker and timeframe it receives. Let’s try it out.

Create A Yii Controller Action

Open up the SiteController.php file and take a look at all of the actions that exist so far. These were created for you as part of the base installation. We’ll go ahead and add a new actionChart method at the end of the file. It will accept two parameters, a $ticker and a $timeframe.


Notice that we set default values in the method to make sure the action works if the user does not provide any data. By default we’ll display a stock chart for the aapl ticker in the daily timeframe. This method allows you to pass any ticker you like, and daily, weekly, or monthly timeframe. Like most other frameworks, once the logic is handled we need to render a view. The render method takes the view file name as it’s first parameter, and an array of key value pairs which will make those values available to the view.

Create A Yii View

So now there is a custom method in the controller that is referencing a view named chart. We don’t yet have a view of that name, so let’s go ahead and create one. In this file we make use of the Html helper in Yii to encode any data that gets passed in from the controller. Note how we set the title of the page using data provided from the controller, as well as the breadcrumbs.


Test Out The Page

We can test out the new page by visiting http://localhost/yii/basic/web/index.php?r=site/chart. It’s worth mentioning how this works. The site in the url is referring to the SiteController.php file and the chart is referring to the actionChart method.

When we visit the url with no parameters, we are greeted with an Apple Daily Stock Chart.
apple daily stock chart

That is pretty slick and look at that, Apple is looking good as it hits new highs. Let’s now test out our parameters. Let’s take a look at the monthly chart for first solar. We can do this by passing this url.


fslr monthly stock chart

Fantastic! Our parameters are getting passed along perfectly and we can see that the monthly chart of first solar is displayed, the title of the page is correct, and the breadcrumbs are updated. Unfortunately the fslr stock itself doesn’t look so hot, coming down from over $300 a share in 2008 to around $55 bucks as of this writing.

Yii Layout File

As we can see, the pages that render look quite a bit more finished than the actual code in the view file would lead us to believe. This is because Yii automatically includes a layout file for the header and footer. The default markup is here, and you could of course highly modify it to meet however you would like it to look.



In this episode we built upon our base installation of the Yii 2 Framework. We created a new action method as well as a new chart view. We were able to pass two variables via query strings and view stock charts of any ticker symbol on the daily, weekly, and monthly scale. Thanks for checking this tutorial out!