Sunday, October 30, 2011

Model-View-Controller (MVC) Yii Framework

Once before I have discussed a bit about Model-View-Controller (MVC) Yii Framework on the post About Yii Framework, now I will discuss more about what else is MVC and how it works.

Yii framework design pattern implements a model-view-controller (MVC), which is widely adopted in Web programming. Model-view-controller (MVC) aims to separate business logic from user interface considerations so that developers can more easily change any part without affecting the others.

Model represents the information (data) and business rules. The model is an instance of a class that CModel or decrease CModel. The model is used to store data and relevant business rules.

A model represents a single data object. Can be either a row in a database table or an html form with user input field. Every field or a data object is represented by an attribute model. Attributes have a label and can be validated against a set of rules.

Yii implements two types of model: Model record form and active. They decrease from the same base class, CModel.

A model form is an instance of CFormModel. The model form is used to store data collected from user input. This data is usually collected, used and then discarded. For example, the login page, we can use the model form to represent the username and password information provided by the user. For more information, please refer to Working with Forms

Active Record (AR) is a design pattern is used for abstracting database access in the form of object-orientation. Each object is an instance of CActiveRecord AR or a sub-class of that class, represents a single row in a database table. The fields in the row can be represented by the property on the object the AR. Detailed information on the AR can be found in Active Record.

View contains user interface elements such as text, input form. View is a PHP script that contains a particular element of the user interface. View could contain PHP code, but code is not recommended to change the data model and should remain relatively simple. In order to maintain the spirit of separation of logic and appearance, a large part of the logic should be placed in the controller instead of a model or view.

View has a name that is used to identify the script file to display when rendering. View the same name as the file name of the script of his view. For example, referring to the file view edit the script file named edit.php. To render view, call CController :: render () with the name of the view. The method will look for view files in the directory corresponding protected / views / ControllerID.

In the script view, we can access the controller using $ this instance. Furthermore, we can draw any controller to evaluate the properties $ this-> PropertyName in the view.

View is divided into 3 parts

Layout. Layout (layout) is a special view that is used to decorate the display. Usually contains the user interface which is common among some of the view. For example, a layout may contain a header and a footer and include the content view.

Widget. Widgets are istance of a class of children CWidget or CWidget. Component that is primarily intended for purposes of appearance. Widgets are usually included in the script view to produce a complex user interface and stand-alone. For example, a calendar widget can be used to prepare a calendar user interface is complex. Widget allows reusability (reuse) the better the user interface.

View System. View refers to the display system used by Yii to display errors and recording (logging) information. For example, when a user requests to a controller or action that does not exist, Yii will raise an exception that describes the error. Yii displays the exception using a particular system view.

View naming system to follow some rules. Names like errorXXX refer to the display to display the error code CHttpException with XXX. For example, if CHttpException raised with error code 404, error404 view will be displayed.

Yii provides a set of default system view that is placed under the framework / views. View-This view can be customized to create the same display file with the same name under protected / views / system.

The controller manages the communication between the model and view. A controller is an instance of the derived class CController or CController. The controller is made by the application when the user to request it. When walking, do the action that the controller is requesting that usually require a model and create the appropriate view. An action, in its simplest form is actually just a controller class method whose name starts with the word action.

The controller has a standard action. When a user requests do not specify where the action is executed, the default action to be executed. The default action is usually named as the index. The default action can be changed by setting the instance variable public, CController :: defaultAction.

2 comments:

  1. if u post a examples of that MVC.its usefull for us.

    ReplyDelete
  2. I really like your article. You’ve mentioned steps which can be easily understood. Just like any other good PHP framework, Yii assists you to develop cutting-edge web applications promptly, and vouch you for better performance.It makes your development job easy by creating secure and testable sites by automating almost all development jobs for you. The features listed in this post will help you do develop the application, customize according to your required needs. There are so many things to know about Yii framework. Check out Yiioverflow to know more about Yii framework and its wonderful feautres.

    ReplyDelete

Yii Framework Tutorials