CakePHP: Understanding the MVC Pattern

CakePHP: Understanding the MVC Pattern

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.


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 defines its relationship with other models. Other than that, the model also defines 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 defined. For example, if we have a model to represent cars, all actions related to it like buy car, sell car etc. should be defined in the model. Models should be the place where the core business logic of an application are defined.


Controllers, in CakePHP, control the application flow 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”.


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.

How It Works

So now that we have a better understanding of all the three components of CakePHP MVC, let’s see how these three components work together to handle a request.

How MCV works

How MCV works

The diagram above shows how it all works together:

  • The request is dispatched to the controller, with user data (POST and GET data).
  • The controller processes the request, and calls the model to access the data.
  • The model responses to the controller’s call, by sending or storing data.
  • The controller then sends the output data to the view.
  • The view outputs the data in the proper format.

As we have already seen earlier, using the MVC pattern helps to structure the code into modular segments. This allows the developer to quickly make any changes if required and as a result, debugging and modification becomes a much easier job. Using MVC also has the added benefit of reusing code. For example, a single model code can be used in any controller that needs to access the data in that model.

Another benefit of MVC is that it results in a shorter development time. But MVC is not the only tool that CakePHP has to quicken the development time.

Pin It on Pinterest

Share This