The MVC principle in web programming

MVC principle in web programming (Model - View - Controller) is one of the most successful ideas to date. The MVC principle is intuitive at first glance, but not very simple when delving into it. First, let's consider what it is intended for.

MVC principle allows the separation of the implementation of application logic, the visual appearance (graphical user interface, GUI), and interaction with the user.

This leads to more structured code, enables specialization in working on the project, simplifies code maintenance, and makes it more logical and understandable. Changes in one component minimally affect the others. Different views and controllers can be connected to the same model.

On the other hand, this requires higher performance of the executing machines, but recently, this is not a big problem - more complex programming solutions require support, and the cost of support will greatly exceed the cost of more powerful modern equipment.

Almost all modern frameworks utilize the MVC principle.

Let's take a closer look at the components.

Model - contains the so-called "business logic" - data processing and verification, database operations, represents the internal structure of the system. The model should not directly interact with the user.

View describes the visual appearance of the application.

Controller - the connecting link between the model and the view, receives data from the user, passes it to the model, receives the processed result, and passes it to the view.

The relationship can be seen in the diagram:

Image Source: http://www.wikipedia.org Component requirements:

Models:

  • should have properties representing specific data;
  • should include business logic (e.g., validation rules) to ensure that the data meets the requirements;
  • can contain code for working with data.
Views:
  • should primarily contain markup, such as HTML, and simple PHP code used for traversal, formatting, and displaying data;
  • should not directly interact with the database. This should be done by the models;
  • should not directly access $_GET, $_POST, and other variables received from the user's request. This task should be performed by the controller. Views should only be used to format data received from the controller and model;
  • can directly access the properties and methods of the controller or models. However, this should only be done for the purpose of displaying data.
Controllers:
  • can access $_GET, $_POST, and other PHP variables received from the user's request;
  • can create instances of models and manage them. For example, in a typical model update action, the controller can first create an instance of the model, then populate it with data from $_POST, and if the model is successfully saved, redirect the user's browser to the page of the created model. It should be noted that the actual saving of the model should be implemented in the model class, not in the controller;
  • should not contain SQL queries. It is better to keep them in the models;
  • should not contain HTML and other markup. It should be moved to the views.
(Requirements taken from: http://yiiframework.ru/doc/guide/ru/basics.best-practices)

In addition to the MVC concept, there are many others, such as MOVE (Models, Operations, Views, and Events) - supposedly an evolution of MVC (taken from: http://habrahabr.ru/post/147038/), but these concepts are less common.