Home » Uncategorized » Yii Framework: Introduction to Code Snippets

Yii Framework: Introduction to Code Snippets

Reading Time: 2 minutes

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.

(Have been liking our posts for a while? Subscribe now! We’ll make sure that you never miss an update. And talk to our developers now if you need professional help with your Web strategy.)

Yii Framework

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”.

Code

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.

Coding

$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 :
$this->widget(‘application.extensions.menu.SMenu’,
array(
“menu”=>$menu,
“stylesheet”=>”menu_blue.css”,
“menuID”=>”myMenu2”,
“delay”=>3
)
);

Time to know how to create drop down list in form

Here’s your code:

Code

Now let’s create some links in form

Coding

How to send form using hyperlinks and confirm?

Let’s make it easy through a code snippet.

coding

How to validate form?

Coding

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.

Varun Bhagat is a technology geek and works in PixelCrayons as a Sr. Technology Consultant. He possesses in-depth knowledge of different web development technology and helps our clients to choose the best technology as per their needs.

Are you ready to turn your idea into reality? Contact Us Now
  • Normally I don’t learn post on blogs, but I wish to say that this write-up very pressured me to take a look at and do it! Your writing style has been surprised me. Thank you, quite great post.