Lets see how to create model, views and controllers in YII. Starting from version 1.1.2, Yii is equipped with a Web-based code generation tool called Gii. By using that you can generate model, views and controllers. First of all you have to connect the databese to your project. Open your project go to protected/config/main.php. See the below screen shot to see how to enable database in project.
Keep remember to uncomment the correct lines. I am using MySQL. So, I remove the comment on that. You can replace the word 'newRally' with your database name. Give the same username and password that you used to access your database.
Now lets see how to enable Gii command in your application. Refer the below screenshot for that.
You can give any password to access the Gii command. To access Gii. in your browser type: http://localhost/testApp/index.php?r=gii. It will ask you the password. Type the password you entered above (According to above it is 'root') and press enter. Now you can use Gii to create models, views and controllers.
Creating a Model
Refer the below screen shot.
Type the database table name that you want to crate a model. Press 'Preview' button and then 'Generate'. It will create a model under 'protected/models' directory. It has attributes, label, relations,etc. You can use this model to handle the database.
Creating Controller & View
See below screen shot.
Type the model name as 'controllerID' . Press on 'Preview' and then 'Generate'. This will create a controller name 'ReplyController' under 'protected/controllers' directory and a view name 'index.php' under 'protected/views/reply' directory.Now you can start working on your web application.
View
- Contain presentational code, such as HTML, and simple PHP code to traverse, format and render data.
- Avoid containing code that performs explicit DB queries. Such code is better placed in models.
- Should avoid direct access to $_GET, $_POST, or other similar variables that represent the end user request. This is the controller's job. The view should be focused on the display and layout of the data provided to it by the controller and/or model, but not attempting to access request variables or the database directly.
- May access properties and methods of controllers and models directly. However, this should be done only for the purpose of presentation.
No comments:
Post a Comment