Can you briefly explain MVC? The simpler the better
滿天的星座
滿天的星座 2017-05-16 17:06:26
0
17
1208

I recently planned to learn the PHP framework, only to find that my previous understanding of MVC was very superficial. But looking at Laravel's documentation, I'm still confused about MVC

滿天的星座
滿天的星座

reply all (17)
刘奇

For example, if you write a Todo List, follow the front-end MVC and write it like this:

  • Model: A JSON array, corresponding to the content in the database, which is text, completion status
  • View: HTML template, or DOM template, which is the interface
  • Controller: The user operates on the interface and then changes the Model

Generally there is such a relationship (not very accurate, and various MVC implementations are not completely consistent):

  • The entire program is updated around Model changes
  • View is rendered according to the Model and updated as the Model is updated
  • Controller receives event triggers in View and modifies Model

It’s a cycle as a whole~ Starting from the Model, adding user operations in the middle, and then returning to the Model
This is an idea for writing graphics, which is to separate the data from the interface and simplify the program.
In other words, abstract the minimum data representing an interface as the Model, and the minimum operations as the Controller.
The View changes with the Model, and can even be changed at will according to user needs.

roughly:

or:

    大家讲道理

    The following answers are personal opinions, please correct me if I am wrong.

    Let’s talk about native PHP first.
    Data processing, pages are displayed together, nested within each other.


    MVC is different, data processing and display are separated.
    When the business is not complicated, you can basically handle it with V and C.
    Brother C processed the data, packaged it and handed it to V. Tell V solemnly: "Brother V, the data is all here, take it and display it!"
    V said: "Okay Brother C!" and then displayed the data on the front end. Brother V also has syntax similar to foreach, which is used to display the data given by Brother C.

    Sometimes the business becomes complicated and Brother C is a little tired of processing data alone, so Brother C calls Brother M to help.
    Brother M helps Brother C do some repetitive work and gives it to Brother C after handling it. Then Brother C will hand it over to Brother V.


    Native PHP is like outsourcing, often one person has to handle the front-end and back-end things.
    MVC is like a mature company, with a front end and a back end. The division of labor is clear.

      阿神

      Everyone has talked about the concept. In fact, the meaning of MVC has been changing subtly. There is a big difference between the original MVC of CS software and the MVC of today's php ruby python. It is even possible that the concept has become MVP long ago, but everyone is used to MVC and has misunderstood it

      I think in terms of actual projects, you can roughly understand the essence and goals of MVC by doing three thought experiments. How to divide the three layers specifically, whether it is three layers, four layers, or two layers, is actually to achieve flexibility and maintainability. It’s just a sexual means

      Change database selection

      The data structure remains unchanged. How much change does your project require when migrating the database from mysql to pgsql or even mongodb?
      The ideal MVC architecture should not need to modify any business code (including Model), only need to modify the configuration file, and at most write a new DBAL driver
      In actual situations, there are subtle differences in the capabilities of different DBs, which should be solved by fine-tuning the Model.

      If your answer is a blind eye: it’s almost the same as rewriting it again, then your M layer is not independent enough, and it’s time to disperse the code written in the Model elsewhere

      Mobile HTML5 version

      Assuming that all functions remain unchanged (all have reasonable and natural mobile version interactions) and add a mobile version to your site, how much change will your project require?
      The answer should be to rewrite a set of Views, and then change the Controller to one lineif(isMobile) use(MobileView);

      If you find that the Controller needs to change a lot of logic, and even the Model is involved, then your V layer is not independent enough

      Added API

      Assuming that all functions remain unchanged, how much change will your project require if you add an open API to your site (for use by third parties or mobile applications)?
      The answer should be a new set of Controller containing new authorization, data format and verification logic, and a simple View (only outputting json or xml)

      If you find that the Model needs to be changed, some things in the original View need to be moved, or part of the code originally written in the old Controller needs to be copied, then your C layer is not independent enough

        刘奇

        The most vivid and simple thing is to compare MVC to the card-based game console we played as a child:
        M: It’s the game card, saves data, is responsible for business logic, etc.
        V: It’s the TV, responsible for presenting the game screen
        C: It is the game control handle, responsible for the interaction between the first two

          大家讲道理

          Take a shopping website as an example.
          M provides a data model. For example, the user data of the website includes user name, password, and email address. Each user can place multiple orders, and each order has an order number, price, etc.
          V provides a view, which is what the HTML interface you see looks like, such as the presentation of product lists and the presentation of personal historical orders.
          C is the controller, responsible for extracting data from M and then presenting it in V. For example, when you want to check your personal order history in the past week, V is responsible for finding all your orders from M, filtering out the order data from a week ago, and then providing the rest to V for display.

            滿天的星座

            The prerequisite for understanding MVC is the concept of code layering, and the purpose of code layering is decoupling.

            Please try to answer two questions:

            1. Why should the code be decoupled?
            2. How to decouple?

            Why code should be decoupled

            A software starts from the user triggering the business requirement on the view, to the program processing the requirement according to certain business logic, and then to the processing result being fed back to the user on the view. The code in the whole process is responsible for three main tasks, namely : View operations, business logic processing, and connection between views and business logic.

            If the code is not layered in the program, the implementation of these three aspects will be coupled in one class. In order to maintain the maintainability, readability, and flexibility of the code (please refer to @mcfog's answer), these codes should be written into different classes according to aspects, and then classes with the same aspects should be put into a package. Classes and packages look like they are on different levels.

            How to decouple

            mvc is a mature layered (decoupled) solution.

            According to the answer to the first question, you need to put different levels of code into different classes (and packages), so how do these different levels of code collaborate?

            Other answers to this question seem to have answered this question specifically and with pictures and texts.

            The code in the Model is responsible for business logic;
            The code in the View is responsible for user interaction;
            The code in the Controller is responsible for the connection between the model and the view.

              習慣沉默

              Model View Controller model, view, controller
              Model: Data model
              View: UI related elements
              Controller: used to connect the model and the view

                世界只因有你

                The simpler, the better, right?

                M: Model - how do you model the data, or what structure is used to represent your data

                C: Controller - how you process business logic. This "processing" mainly corresponds to two ends: one end is to request the data source required for processing from the model; the other end is to pass the processing results to the view in some way; The specific process in the middle is the level that the controller is responsible for

                V: View - how you present the results of business processing to the client and provide interaction

                In fact, it is not good to say it too simply, because some details can only be highly summarized and abstracted for simplicity. Without sufficient knowledge and experience to support it, it is like seeing flowers in the fog.

                  洪涛

                  After reading everyone’s answers above, I finally realized: V is for users to see, C is for users to control, and M is inaccessible to users. Can M be controlled through C and V?

                  Discussion about MVC on SO: What is MVC, really?

                    phpcn_u1582

                    To put it simply, don’t mix thedatabase (Model)and thedata display (View)code together. If there is some business logic that needs to be added, it is theController (Controller)

                      Latest Downloads
                      More>
                      Web Effects
                      Website Source Code
                      Website Materials
                      Front End Template
                      About us Disclaimer Sitemap
                      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!