Getting Started with the Yii Framework in 8 Quick Steps

Everybody is jumping on to the Yii bandwagon. Yii is the new performance-intensive, reusable and extensible component-based PHP framework for developing large-scale Web applications. This quick overview of different facets of Yii programming put together by the Yii team at Maven Infosoft (Maven Infosoft) will help you get a ground up on the Yii framework.

This overview is for beginner-to-intermediate PHP developers who want to build Web 2.0 applications based on the PHP framework. Read on for useful snippets of knowledge of the Yii framework…

=> Starting Point

* To create any sample application using the Yii framework, you first need to create an index.php file. This file in turn requires and references the yii.php file which is located at framework/yii.php.
* To start the framework process there should be some code in index.php that will start the process on your entered URL and posted data. This code is as follows:

Yii::createWebApplication()->run();

=> MVC Structure

* A simple file structure is used for MVC

* There should be a folder called protected – this is the standard folder name for storing all controllers, models, views, configuration, data and other folders.

=> Controller & Action Search

* Just like other frameworks, this one also searches for controllers using URL path.
* Every controller should be extended using CController, which is a standard Yii class.
* For example, if my URL is http://dev.yii.com/demos/blog/index.php/post/show/id/21, then the framework will search for a PostController.php file in the protected/controllers folder.

The argument after the post is the name of the action. The controller PostController.php be searched for an actionShow() function. Remember that the default action for every controller is “index”. So if you don’t specify any action name, then it will search for an actionIndex() function.

After the controllerID and actionID, you can see the arguments /id/21. These are the arguments which are passed through URL.

=> Rendering Layouts

* HTML Views files should be stored under, protected/views/controllerName/
* HTML files are stored with a PHP extension e.g. show.php
* To render any layout on any action, you need to include

$this->render(‘show’,arrayParameters);

The arrayParameters are optional.

* Remember, files in views and controllers have direct contact with each other. So it is possible to call any function of a controller file, because $this instance passes to view files by default.

=> Configuring the Main.php file

* The main.php file is used to override the default configuration of the Yii framework.
* You can modify the base path, name, database details, etc.
* To connect to your database, place the following code into main.php:

‘db’=>array(
‘connectionString’ => ‘mysql:host=localhost;dbname=blog’,
‘emulatePrepare’ => true,
‘username’ => ‘root’,
‘password’ => ‘ rootroot ‘,
‘charset’ => ‘utf8′,
‘tablePrefix’ => ‘tbl_’,
)

=> Calling Layouts

* To call any layouts from any controller, you just need to define one variable in your controller file as follows:

public $layout=’column2′;

* The framework will search for this layout in protected/views/layouts/column2.php.
* It’s also possible to call any layout from a layout file. For example, if you want to call your column1.php file into column2.php, you can use:

beginContent(‘/layouts/column1.php’); ?>
endContent(); ?>

=> Database Active Record Model

* A database is defined as object in the Yii framework.
* An AR (Active Record) model system is used to achieve this type of structure. So every record also counts as an object.
* Models should be stored in protected/models.

=> Creating Forms

* To create a form, there should be a corresponding model file in the protected/models folder. Further, the class should be extended using CFormModel, a standard form class.
* Validation is done using the rules() function. Here’s an example:

public function rules()
{
return array(
// username and password are required
array(‘username, password’, ‘required’),
// rememberMe needs to be a boolean
array(‘rememberMe’, ‘boolean’),
// password needs to be authenticated
array(‘password’, ‘authenticate’),
);
}

Maven Infosoft has developed scores of enterprise applications based on OOP-based frameworks and have an extensive knowledge base of PHP frameworks built using OOP and MVC. Please Contact with us for any technical support or queries regarding Yii. RFIs / RFPs are solicited for web applications running Yii.

Related Post