Posted by: jogisilalahi on: July 23, 2009
The MVC (Model View Controller) pattern is a commonly used design pattern in software development, where the code is separated into three major parts:
models, views, and controllers.
The exact purpose of each part depends on the implementation, as it may vary from one framework to another. Here, we are going to describe the way CakePHP implements the MVC pattern. So, this is not a general discussion on MVC pattern, rather we are only going to see Cake’s own MVC implementation. As we have already mentioned, CakePHP separates the code into three separate parts: models, views, and controllers.
Models
In CakePHP, a model represents a particular database table. Each database table should have a model representing it. So, in CakePHP, every database table has its own model. All PHP code related to accessing, adding, modifying or deleting records from the table are situated in the model. The model also contains code that deines its relationship with other models. Other than that, the model also deines the data validation rules when adding or updating data for that model. Model can be thought of as the data layer of the application. The model is also the place where the business logic related to the model should be deined. For example, if we have a model to represent cars, all actions related to it like buy car, sell car etc. should be deined in the model. Models should be the place where the core business logic of an application are deined.
Controllers
Controllers, in CakePHP, control the application low or logic of the application. Each web request is directed to a particular controller where the user input (POST or GET data) is accepted. The controller logic then decides what response is generated. The controller logic normally contains calls to models to access data, and also other functionalities like access control check etc. Lastly, the controller passes the response (output) to the view (discussed next). Controller can be thought as the control logic layer of the application. As mentioned above, the model should have all the business logic of an application. The controllers should just delegate the actions to the model, and be light. This design philosophy is sometimes referred to as the “fat models and thin controllers”.Chapter 1
Views
Views are the outputs or responses that are sent back to the user once a request is processed. They basically consists of markup (like HTML) code with embedded PHP code, but they can also be other forms of output like XML, PDF document etc. depending on the situation. Views can be thought as the presentation layer of the application.
taken from :
CakePHP Application Development
recent comment