Starting out with Yii Framework


You can check a comparison of different PHP frameworks here. Some of the more popular ones are Zend, CakePHP, CodeIgniter, Sympony. One of the new and promising one is Yii. This I believe is still in beta but it is very promising in terms of speed, features and usability. I have worked with Zend before as a starter and it was too complex to get hold of. Although all PHP frameworks and based on principle sof M-V-C (Modeal, View, Controller), some of theme are perhaps easier to work with. Notice the following statement from Larry Ulman about Zend Framework

In 2009, I had three decent-size Web sites to develop, so I thought I might try using a PHP framework for the first time, instead of coding everything from scratch. I’ve used Ruby on Rails for Web development before, so I’m comfortable with frameworks and the MVC architecture, but I wanted to educate myself on PHP frameworks. After researching a handful of frameworks, and after an unsatisfying attempt to use Zend Framework, I finally settled on, and really came to appreciate the Yii Framework.

With that said, you should feel comfortable with Yii and may be dwelve into it. Lets begin with the basic tutorial of Yii.

1. First Download Yii from here. Make sure select the zip file in the first column. At the moment version 1.1.8 is the latest stable release.

2. Installation. Simply unzip the downloaded file and place it in htdoc folter under Yii Name.

3. Not that for the above two to work properly, you need to install PHP, MYSQL and Apache Web server on your local machine. This is not frightening. I recommend XAMPP which contains all the three in one installation. You can download it from here.

The next step it running Yii for the first time. It is more of building your first application with Yii.

Yii uses command line (console) utilities to setup your project. It also uses Gui based utilities once you setup the project. I am not sure why Yii uses command line utility but its advantage is, it is light weight, quick to build (not time wasting on GUI) and it works the same as a GUI, in fact it might work better. So we are going to start the command line tools that Yii provides us.

The file that we need to look at is yiic.bat. I had trouble running this on Windows even with using the .bat extension so we will do something simpler. Simple do this. Click start at the left bottom->Right click on “My Computer”->Click properties. Now select advance tab and then select “Environment Variables”, one of the two buttons in the bottom. You will see a screen like this

Classes in Yii Framework - Block Diagram

Select in Path variable and click edit. You will be prompted another window to enter the new path that you want to modify. Do not modify the existing path, except just add “;C:\xampp\htdocs\yii\framework” after the end of the text. Do not forget the semicolon. The is the folder in which my Yiic.bat file resides. If your is different (most likely), use the path that you have in your folder. By doing this we are allowing access to Yiic.bat from anywhere in the command prompt.


Part 2

How to build your first Application

we are going to use Yii command line too, Yiic.bat to build the application. First we need to go to the command prompt. Click start -> Run and type “cmd”, now enter. You will go into command prompt with a default path. To change the path go to the folder where you installed/copied your Yii framework. by using cd command. I am using

cd\ to go to root folder than

cd xampp\htdocs\yii, to go to the folder where I installed yii. While here issue this command

yiic webapp first

if this command runs successull you will see a bunch of file created in the current folder in directory name first. Now you can go to widows explorer to view all these files and what they do. A good starting tutorial is video cast by Yii

Part 3

Understanding Yii and MVC

Yii is based on M-V-C. Model – View – Controller. This architecture is uses by all most all PHP frameworks. This is kind of similar to Microsoft Foundation Classes (MFC) but really not the same. In MFC, there is  view – document architecture used. This is to keep the Presentation of the data separate from the logic of the program. Logic is stored in Document while View only represents data. This is one approach of data and logic seperation. MVC is another way to do the same.

MVC is an Object Oriented Framework in which the code consists of classes. So everything we study here is either a class or function in the class.

Modal – Modal is a class that contains your data. It is also the business logic of your application, how to retrieve data from the database, how to collect data from different parts. So whenever you need data, you will also store it in your modal class, not in View or Controller.

View – Is the actually page that presents that data. It is kind of template. It does not contain any data. Data is fetched from the modal depending on what the controller what to display.

Controller – is like schedular, who is working which shift. It basically routes data from Modal to the View. It is perhaps the essential part that connect the user to the application and how a user interacts with your application. If a user click this button, the controller is the first class which detects it. It picks what data needs to be fetched and it decide, which view to invoke. Depending on this a user interaction is catched by the controller.

This is not going to be that easy. To understand this fully you need to understand Object Oriented Programming in PHP and different syntax. It would be best if you can start a couple of your own applications.

Part 4

Yii Framework – Block Diagram

It would be helpful to look into the yii framework, what type of classes it is composed of. The diagram below show all the classes that are part of yii framework.


Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s