Home > Glossary > Yii Framework

Yii Framework

Introduction to the Yii Framework

As PHP Developers we want to be able to write applications as fast as we can and in the easiest way possible.

Nowadays, frameworks and libraries have taken over the web development landscape. Now we can see just about any framework and library for any language. In Javascript land, we have jQuery, Mootools, Backbone, Angular and many more. In Ruby land, we have Rails and Sinatra, to name a few.

PHP is no exception. We have CakePHP, CodeIgniter, Laravel, FuelPHP and many many more. This book is about the Yii Framework, a high-performance component-based web framework designed for developing web 2.0 applications.

What can the Yii Framework do for you?

Stable Codebase

The Yii Framework allows developers make use of code that is well-tested by hundreds and probably thousands of developers in real-world applications. Most developers won’t have the need to write code because a big percentage of the functionalities that developers would be needing would already be available in the framework.


The Yii Framework has been tested by hundreds and a lot of things have been developed in a way to make applications developed with Yii secure.

Write code for you

The Yii Framework comes with built-in code generators to make life easier. CRUD can readily be generated with information provided. Ruby on Rails is known for its ability to create scaffolds.

As with other frameworks like CakePHP and Laravel, the Yii Framework makes use of its code generators to create Controllers, Models and many more components.

Code Reuse

Not all kinds of libraries and classes will be included in the Yii Framework. Email classes and Image manipulation libraries are some examples of classes and libraries that aren’t included. However, we could easily include these within the Yii Framework as extensions.

Use of GridViews

The above items can be said with any of the other frameworks. Most frameworks helps in securing your application, are stable enough and allows code reuse. The Yii Framework, however, allows you to use its built-in GridViews. It allows you to generate a sortable, paginated table ala datatable

Comparing the Yii Framework to other Frameworks

We all want the best. Developers like to work with the best. This has plagued thousands of developers around the world. Sometimes this has caused analysis paralysis.

In the previous chapter, we discussed that there are other PHP frameworks out there. Yii wasn’t the first PHP framework.


One of the earliest frameworks along with Symfony. CakePHP adheres to a convention over configuration approach just as Ruby on Rails. The convention over configuration approach basically means that although conventions within the framework are overridable, these conventions preferably should be followed.

CakePHP is stricter when it comes to conventions than say CodeIgniter or the Yii framework. Depending on the developer, stricter conventions is a big plus or minus.


CodeIgniter is a lightweight framework. It has come to be one of the fastest framework and has always been tested against newer frameworks because of its speed. CodeIgniter has less conventions and configurations than CakePHP or Yii. It is a preferable framework if you are new to PHP and frameworks altogether because of its extremely useful documentation.


Kohana started as the PHP5 version of CodeIgniter. It’s definitely slower than CodeIgniter but makes use of better OOP principles than CodeIgniter.

Kohana introduced the concept of HMVC, a variation of Model–view–controller (MVC) and Cascading File System.

The Cascading File System is a very interesting concept where in you can override configuration and logic by creating files higher up the hierarchical ladder. In Kohana, the hierarchical ladder is:

  • Application Path – Your main application files
  • Modules Path – Module files
  • System Path – Kohana files

What Framework should you choose

There is no best framework. You can see thousands of comparisons between microframeworks to the full-fledged battery-included frameworks:

The truth is, none of these matter since you have to choose what framework you are comfortable with. Some frameworks like the Yii Framework has jQuery built-in. Some may consider that a plus or a minus. Some frameworks like Silex have very few things and you would need to create or include what you need for your projects.

Exploring the Yii Framework File Structure

As you can see, installing the Yii Framework is very easy. Now let’s take a look at the files we have.

The initial files and folders we can see are:

  • assets — Yii generates files and resources like images and javascript files and store them here.
  • protected — This holds your application files. You will spend most of your time working on files here.
  • css
  • themes — Yii comes with a themes feature. Theme files will be located here.
  • index-test.php
  • index.php — The boostrap file or the front controller. This file will handle all requests.

The index.php file is the most important file. It acts as the front controller which will initially handle inclusion of the other files. The protected folder is where most of your development will be.

Protected Folder

There are more file and folders. If you opened the protected files, you will be able to see a lot more folders which will seem familiar if you worked with other PHP frameworks.

  • commands
  • components
  • config — Your application config files will stored here
  • controllers — Controllers will be here
  • data — If using SQLite, your database file will be stored here.
  • extensions — Third-party extensions, not included in the core, will be stored here.
  • messages
  • migrations — Database migrations for database changes will be included here.
  • models — This folder would include models
  • runtime
  • tests — Unit tests will be stored here
  • views — This folder contains your application views
  • yiic.bat
  • yiic.php
  • yiic

The Main Config File

The config folder includes the main.php file which includes most configuration. Let’s explore the many configurations present:

Understanding the Model View Controller Pattern

The Yii Framework follows the MVC software design pattern. Programming in the Model View Controller pattern separates your application into three major parts.


The Model layer represents the part of your application that implements business logic. It is also usually taken as representation data by many primarily databases, but also XML, JSON and many others. Most interactions with data is carried here like processing, validating and other tasks.


The View Layer represents the part of your application that is viewable by your users. In the view, information is presented to the user via text, images, and forms usually in HTML format. Views, ideally should not have programming logic.


The Controller Layer accepts requests. It also is responsible for presenting responses back to the user with the help of the Model and View layers.

Read more about Yii Framework Controllers


Why use MVC in the first place? The Model View Controller is a very well tested and proven architectural design pattern that makes applications built on it become more maintainable and modular. Separating concerns into models, views and controllers make development a breeze. Adding functionalities are easier.

This makes for the Yii Framework become a rapid application development PHP framework.

Yii Framework URLs

By default, The Yii framework’s URLs are not search engine and human friendly. It uses the typical query string to pass along information to the front controller which is the index.php file.

A typical URL in the Yii Framework represents the controller, the action and any parameters.

  • posts represents the controller
  • view represents the controller method or action
  • id represents a parameter with the value of 2

The Yii Framework contains functions to work with the your URL:

Getting the current controller

echo $this->id
// posts
echo Yii::app()->controller->id
// posts
echo Yii::app()->getController()->getId()
// posts

Getting the current action

echo $this->action->id
// view
echo Yii::app()->controller->action->id
// view

Create a URL

echo Yii::app()->createUrl('posts/view');
// index.php?r=posts/view

Create Absolute URL

echo Yii::app()->createAbsoluteUrl('posts/view');
// http://example.com/index.php?r=posts/view

Removing the index.php file

By default, the index.php is present in Yii Framework URLs. For example:



The index.php file can easily be removed from the URL using an .htaccess file. Here is an example of a file that directly forwards parameters to the index.php without the actual index.php file

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

Main Configuration

You will also need to configure the main configuration array found at protected/config/main.php


With all these changes, we will have a nice URL without the index.php. The side effect is the you will have clean urls like:


Custom URLs