Yii Framework: Introduction to Code Snippets

Coming from our last post that discussed some of the basics of Yii framework, here we’ll give you a brief of Model-View-Controller (MVC) architecture and will straight away steer you into a how-to guide for the following:

1. Creating your first controller

2. Creating drop down menu

3. Creating drop down list in form

4. Creating links in form

5. Sending form using hyperlink & confirm

6. Form Validation

MVC architecture has been adopted by almost all popular web application frameworks, including Yii. Model (M) is basically designed to pen down the relationship among data in database (DB) and to form the guidelines that a user needs to keep in mind while saving data. View (V), which uses HTML, CSS etc, as the name suggests, is for showing saved data to user. Controller (C) is to process and manage data and is basically a doer.

Creating your first controller: Not as complex as you are thinking

To begin with, make one file in folder (protected/controllers) and name it “NameController.php”. You must have noticed that we have put the first letter of both name and controller in upper case. That’s because it is mandatory in Linux servers. Suppose you’ve named the controller as “Rocket”. The file name then would be: “RocketController.php”.

Creating drop down menu: Even simpler

Here we’ll provide you a couple of code snippets, with each creating two menus (each menu will have two sub menus). The first one is faster, while the second one is more articulate, but slower.

$menu = array();
//Create each menu array
$Menu_1[‘url’][‘route’] = “/product/create”;
$Menu_1[‘label’] = “Top Menu1″;
$Menu_1[‘icon’] = “protected/images/menu.png”;

$SubmenuOf1[‘url’][‘route’] = “/product/create”;
$SubmenuOf1[‘label’] = “Submenu of 1″;
$aSubmenuOf1[‘url’][‘route’] = “/product/list”;
$aSubmenuOf1[‘label’] = “Submenu of 1″;

$Menu_2[‘url’] = “”;
$Menu_2[‘label’] = “Top Menu2″;

$SubmenuOf2[‘url’][‘link’] = “http://www.domainname.com”;
$SubmenuOf2[‘url’][‘htmlOptions’][‘target’] = “_BLANK”;
$SubmenuOf2[‘label’] = “Submenu of 2″;

$aSubmenuOf2[‘url’][‘route’] = “/site/spinnerDoc”;
$aSubmenuOf2[‘label’] = “Submenu of 2″;

//Attach 2nd level menus to top menus

$Menu_1[] = $SubmenuOf1;
$Menu_1[] = $aSubmenuOf1;

$Menu_2[] = $SubmenuOf2;
$Menu_2[] = $aSubmenuOf2;

//Attach top menus to menu array
$menu[] = $Menu_1;
$menu[] = $Menu_2;

// And then :

Time to know how to create drop down list in form

Here’s your code:

Now let’s create some links in form

How to send form using hyperlinks and confirm?

Let’s make it easy through a code snippet.

How to validate form?

In conclusion, we hope that the things discussed in this post are clear to you, or else you are always welcome to put forth your queries in the comment section below.



Leave a Reply

Please log in using one of these methods to post your comment:

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