Getting Started With The Yii Framework in 8 Quick Steps
Getting Started With The Yii Framework in 8 Quick Steps
Getting Started With The Yii Framework in 8 Quick Steps
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
* 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();
* There should be a folder called protected this is the standard folder name for storing all
controllers, models, views, configuration, data and other folders.
* 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 dont 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.
* 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.
* 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_,
)
* To call any layouts from any controller, you just need to define one variable in your
controller file as follows:
public $layout=column2;
beginContent(/layouts/column1.php); ?>
endContent(); ?>
* 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. Heres 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),
);
}