w3programmers - An introduction to Yii framework- part 2

An introduction to Yii framework- part 2

An introduction to Yii framework- part 2

In my First article  tutorial I forgot write one important information. That is the pronunciation of Yii. Actually Yii should not be pronounced as Y i i  rather it should be pronounced as English letter e .

OK ! Let’s start. In my last topic I discussed about how simply we can install Yii framework. Certainly it requires very less code to install fully workable application. Our main topic will be to display the word Hello World by using Yii. I will try my best to make the topic as simple as possible.

Before going to display Hello world I want talk about 3-4 lines about MVC. I will talk about it briefly here because it is not possible to discuss everything about MVC in this article. MVC requires a full article to describe it completely. Anyway, I hope you will study about it yourself after getting the primary concept from this article.


MVC stands for Model-View-Controller. Now I’ll discuss about Model, VIEW and Controller


Model is responsible for maintaining the state. It should encapsulate the business rules that apply to the data that defines this state. In a nutshell it refers to the database of your application. It encapsulates all the logic for form field validation and any other business logic that may need to be applied to the form field data. It can then store this data in memory, or with the help of an active record model, store data in a database.


A view in Yii is a PHP script that contains user interface related elements, often built using HTML, but can also contain PHP statements. In simple view is the output or display of our application.


Controller is the main hero of the MVC. We can compare it with our brain. It controls both the model and the view to process and display the output as per the request of the user. Model and View these two different things are stitched together by the Controller. Controller is the coordinator in between Model and View. When a controller runs, it performs the requested action, which then interacts with needed models and renders an appropriate view. I think below diagram explains all more elaborately then my explanations above!!

YII Structure

Now come to the main point. In part one of this tutorial series we created a web application named testyii. So I will use that same application here to display Hello World! by using Yii. So if you go to your testyii application folder you will see that below folders have been created there –

  1. Assets – it contains published resource files.
  2. CSS- Contains all CSS files.
  3. Images – Contains images files.
  4. Protected – Contains protected application files. And
  5. Themes – Contains application themes.

YII Folder Structure

Here protected folder will be used most. Because we have to do maximum operation in this single folder.  Inside this protected folder our three MVC brothers exist.


Right now we will use only Controller and View folder. In later topics we will use the three together as per requirement.  So to display Hello World! we need to create the controller first to control everything.

To create the controller we need to use Command Prompt (CMD). To create the controller we have to use Yii shell command in CMD. Please follow below steps accordingly to create the controller.

  1. Enter your webroot (testyii). It will now look like- C:\xampp\htdocs\testyii>
    Create YII Controller
  2. To enter Yii’s shell command type yiic shell  and press enter
  3. YII Command Type

Now you will see that your cursor is blinking beside >>. If you see this then you can become sure that you have successfully have enter inside the shell command of Yii. Great. Now it’s the high time to create our controller. You can also be sure to whether you have entered into the shell command or not just by typing ‘help’. After typing help press enter and lot of things will come in front of your eyes. The main item names are given below. You can use all of them for command for different purpose. For example we will use the “controller” soon.


Create YII Controller with Different Purpose3. Now type controller message helloworld to create a controller. It will display that controller has been created. Here message is the name of the controller and “helloworld” is the action ID of the controller “message”. It will also tell you how can access this controller in your browser. Just simple.

YII Controller Message With Hello World

Ok. Now lets check whether our controller has been created or not. For this follow the below steps –

  1. Go to your testyii application folder.
  2. Enter into protected folder.
  3. Inside protected folder you will find controller folder.
  4. Enter into controller folder and you will find your message controller there with the name MessageController.php

YII Message Controller File

We have created our controller successfully. The generated MessageController.php extends the base framework class cController, so it automatically gets all of the default controller behavior. As we have mentioned an actionID(helloworld) parameter, a simple action, was also created within MessageController named actionHelloWorld().  To see this actionHelloWorld() function- open the MesssageController.php file in your editor and you will find below codes there-

class MessageController extends Controller{

public function actionHellowrld() {
public function actionIndex()


The yiic command assumed that this action, like most actions defined by controller, will need to render a view. So it added the code to this method to render a view file by same name helloworld.php. You will find this helloworld.php in View folder. Lets check –

  1. Enter into protected folder.
  2. Enter into views folder.
  3. Finally enter into message folder and there you will see this helloworld.php.

YII Hello World

  1. When you will open this file you will find below code there


<h1><?php echo $this->id . '/' . $this->action->id; ?></h1>


<b><p>You may change the content of this page by modifying the file <tt><?php echo __FILE__; ?></tt>.</p>

I am very much near to close of this boring lecture.  To display the “Hello World!” you need to do just one more thing. Go to your editor (IDE) open the file helloworld.php  delete all the items below the php script and just write <h1>Hello world!</h1>  then save and exit. The code will be as below-





<h1>Hello World! </h1>

Now open your browser, type http://localhost/testyii/index.php?r=message/helloworld and  see the magic.

YII Final Output

Thanks for your extreme patience to continue this article up to this line. It will be my pleasure to hear from you.

One comment to “An introduction to Yii framework- part 2”

Leave a Reply

Your email address will not be published. Required fields are marked *