In this tutorial we discuss about the basic principal of MVC using Yii Framework. How to create Model extends from CFormModel which we collect data from it, to create related Controller and finishing at View.
1. Generate webapp from Command Prompt
yiic webapp myapps
2. Uncomment gii on myapps/protected/config/main.php. Set your prefered password. This allow us to use scaffolding modules provided by Yii Framework. Please remove this when you ready to deploy your application for security reason.
3. Access your gii module at your browser
http://localhost/myapps/index.php?r=gii.
4. Using gii module online forms, create a Controller, name it MyData, it should generate MyDataController.php. You can access it at root/myapps/controllers/MyDataController.php
5. Duplicate myapps/models/ContactForm.php, rename it as MyDataForm.php. This will act as your model. This model extends CFormModel class as we dont use Active Records.
6. Clean up MyDataForm.php like this
[php]
/**
* ContactForm class.
* ContactForm is the data structure for keeping
* contact form data. It is used by the ‘contact’ action of ‘SiteController’.
*/
class MyDataForm extends CFormModel
{
public $name;
public $email;
public $subject;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array(‘name, email, subject’,’required’),
);
}
}[/php]