Thursday, September 11, 2014

MVC Design Pattern in YII

Yii implements the model-view-controller (MVC) design pattern, which is widely adopted in Web programming. Before start working on your web application, lets see what is MVC.

MVC simply means model-view-controller. MVC aims to separate business logic from user interface considerations, so that developers can more easily change each part without affecting the other. In MVC, the model represents the information (the data) and the business rules; the view contains elements of the user interface such as text, form inputs; and the controller manages the communication between the model and the view. see the below image to understand the MVC.



























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.

Controller

  • May access $_GET, $_POST and other PHP variables that represent user requests.
  • May create model instances and manage their life cycles. For example, in a typical model update action, the controller may first create the model instance; then populate the model with the user input from $_POST; after saving the model successfully, the controller may redirect the user browser to the model detail page. Note that the actual implementation of saving a model should be located in the model instead of the controller.
  • should avoid containing embedded SQL statements, which are better kept in models.
  • should avoid containing any HTML or any other presentational markup. This is better kept in views.

No comments:

Post a Comment