What is MVC (Model, View, Controller)?


After all, how does this architectural pattern work following the model, view, and controller layers?

asked by anonymous 23.03.2015 / 20:39

1 answer


MVC is nothing more than a software architecture standard, separating your application into 3 layers. The layer of user interaction , the data manipulation layer , and the ).


Whenever you think of data manipulation, think of model. He is responsible for reading and writing data, as well as for his validations.


Simple: the user interaction layer. It only displays the data.


The responsible for receiving all user requests. Its methods called actions are responsible for a page, controlling which model to use and which view will be shown to the user.

(The image below represents the MVC stream in an Internet context, with an HTTP request and response in HTML or XML format)







OBS:ThistextwastakenfrommyTablelesssitearticle: MVC, After all, what is it?

23.03.2015 / 20:42

In theory, MVC is a programming standard that separates the application into 3 parts / layers: M odel, V iew and C > ontroller ( model, view and free-translation controller ).

But in practice, usually, there are several doubts. For example: "what and how to develop each application layer?"

Often diagrams about MVC omit a very important part that makes the entire MVC work. Some call it bootstrap , others in init and so on ... The important thing is to understand that this part is the root of MVC!

Look at this diagram I did:

  • Instep#1theuserrequestsapage/complementofyourapplication.Example:http://mvc.meuapp.local/;

  • Instep#2yourapplicationstartstopreparetheserver(settings), pertaining to the application (ignoring user exceptions), and so on. This is the root of your application that ultimately calls the controller and method according to the URI ;

  • In step # 3 the controller calls all add-ons related to the user's request. This is where we get the parameters ( GET / POST ), literally call and control the actions of the models;

  • The step # 4 represents the response (return) of the actions of the models, which in turn are delivered back to the controller;

  • Finally, in step # 6 the information is rendered to the user. When you have a default layout for the entire application, this is where you couple the content to be rendered to it.

Finally, I would say that MVC is composed of 4 parts / layers: application (bootstrap / init), controllers, templates and renderings / visualizations.

Many beginners (including me) start producing the MVC thinking that every controller has its own unique model. But not quite!

In the case of MVC, we can interpret "models" as the classes that instantiate the various objects of our application, for example, in a company: customers, suppliers, employees, products and etc ...

There is even a MODELless controller! Imagine you have a static page that there is no need to treat data, access databases and etc ... Just return a text to the user. No model required. Just render the view and that's it!

Now, see this example of an MVC application - with a single product module:

 _  Aplicação
    \_  Controladores       (controlador)
          • Produtos
            -> listar      (métodos do controlador)
            -> buscar
            -> cadastrar
            -> ver
            -> encomendar
            -> vender
    \_  Modelos:            (classes relacionadas aos produtos - omiti os supostos métodos)
        • Produto
        • Funcionario
        • Fornecedor
        • Fretista
        • Cliente
        • Db
    \_ Renderizações/visualizações:       (uma visualização para cada método do controlador - não é regra)
        \_ Produtos
            • listar
            • buscar
            • cadastrar
            • ver
            • encomendar
    • App                   (classe que extende o bootstrap - constrói-se aqui os métodos referentes a configurações em acordo com o projeto em si)
 _  vendor
    • Bootstrap/init
    • Controlador           (classe abstrata com os métodos comuns)
    • Modelo                (classe com os métodos comuns entre os modelos)
    • AutoLoader            (carrega as classes - necessário, por exemplo, no PHP)
 _ public_html
    \_ javascripts
    \_ imagens
    \_ estilos
    \_ fontes
    \_ ... (etc)
    • index                 (carrega o autoloader e instancia a aplicação)


_   = pastas dentro da pasta raiz;
\_  = subpasta;
•   = arquivo;
->  = método da classe;

Final considerations:


The entire text of this answer is my own. These concepts I got by merging the knowledge gained by reading various articles and video-lessons. This explains the terms " aportuguesados " ... If something is not within the , just comment that I will review and edit this answer (it's more for an article) / p>      

I tried to be as objective as possible, without much "bla bla bla", in order to explain the basics;


Anti-haters: This response complements the @Allan Ramos in this same question and does not replace it;

06.01.2019 / 03:25