What is MVC? _ PHP Tutorial

Source: Internet
Author: User
What is MVC. What is MVC? I remember the first interview with phper (php is another web development language that can be quickly used for me). people asked me about MVC. I only know that m is model and v is view, c is Controller. what is MVC?

I remember the first interview with phper (php is another web development language that can be quickly used for me). people asked me about MVC. I only know that m is model and v is view, c is the Controller. I know nothing about other things. as a result, I was asked questions !! I know how bad I am. So I decided not to give up on it! Find the following information:

2006-12-17 12:02:37

Large, medium, and small
Model-view-controller (MVC) is a software design model invented by Xerox PARC for programming language Smalltalk-80 in 1980s. it has been widely used. Sun's J2EE platform design model has been recommended in recent years and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-Controller mode is a useful toolbox, which has many advantages but also has some disadvantages.
How MVC works

MVC is a design pattern that forcibly separates the input, processing, and output of an application. MVC applications are divided into three core components: Model, view, and controller. They process their own tasks.

View
A view is the interface on which the user sees and interacts with it. For older Web applications, a view is an interface composed of HTML elements. in new Web applications, HTML still plays an important role in the view, however, some new technologies have emerged, including Macromedia Flash and some identification languages such as XHTML, XML/XSL, and WML and Web services.

How to process the application interface becomes increasingly challenging. One major benefit of MVC is that it can process many different views for your applications. In fact, there is no real processing in the view. whether the data is stored online or an employee list, as a view, it is just a way to output data and allow users to manipulate it.

Model
The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects such as EJBs and ColdFusion Components to process databases. The data returned by the model is neutral, that is, the model has nothing to do with the data format, so that a model can provide data for multiple views. Because the code applied to the model can be reused by multiple views only once, code duplication is reduced.

Controller
The controller accepts user input and calls models and views to fulfill user requirements. Therefore, when you click a hyperlink on a Web page and send an HTML form, the controller does not output anything or process anything. It only receives the request and determines which model component is called to process the request, and then determines which view is used to display the data returned by the model for processing.

Now Let's summarize the MVC processing process. First, the controller receives user requests and decides which model should be called for processing. then, the model uses the business logic to process user requests and return data, finally, the controller uses the corresponding view to format the data returned by the model and presents it to the user through the presentation layer.



Why use MVC?

Most Web applications are created using procedural languages such as ASP, PHP, and CFML. They mix data-layer code such as database query statements and presentation-layer code such as HTML. Experienced developers will separate the data from the presentation layer, but this is usually not easy to do. it requires careful planning and continuous attempts. MVC strictly separates them. Although constructing an MVC application requires some additional work, the benefits it brings to us are unquestionable.

First, the most important thing is that multiple views can share a model. As I mentioned, you need to access your application in more and more ways. In this regard, one solution is to use MVC, whether your users want the Flash interface or WAP interface, they can be processed using a model. Since you have separated data from business rules from the presentation layer, you can reuse your code to the maximum extent.

Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be represented in HTML, but they may also be represented in Macromedia Flash and WAP. The model also provides state management and data persistence processing functions. for example, session-based shopping carts and e-commerce processes can also be reused by Flash websites or wireless networking applications.

Because the model is self-contained and separated from the controller and view, it is easy to change the data layer and business rules of your application. If you want to port your database from MySQL to Oracle, or change your RDBMS-based data source to LDAP, you only need to change your model. Once you implement the model correctly, the view will display the data correctly regardless of whether the data comes from the database or the LDAP server. Because the three components of an application using MVC are opposite to each other, changing one of them will not affect the other two, you can build a loose coupling component based on this design idea.

For me, the controller also provides the benefit of using controllers to connect different models and views to meet user needs, in this way, the controller can provide a powerful means to construct an application. Given some reusable models and views, the controller can select a model for processing based on the user's needs, and then select a view to display the processing results to the user.

Disadvantages of MVC
The disadvantage of MVC is that it is not easy to fully understand MVC because it is not clearly defined. Using MVC requires careful planning. because of its complicated internal principles, it takes some time to think about it.

You will have to spend a considerable amount of time thinking about how to apply MVC to your application, and because the model and view must be strictly separated, this also makes it difficult to debug the application. Each component must be thoroughly tested before use. Once your components have been tested, you can reuse them without any scruples.

In my personal experience, since we split an application into three parts, using MVC also means that you are going to manage more files than before, which is obvious. It seems that our workload has increased, but remember that this is not worth mentioning compared to the benefits it can bring us.

MVC is not suitable for small or medium-sized applications. it is usually not worth the candle if you spend a lot of time applying MVC to a small or medium-sized application.

MVC is a good way to create software
The MVC design pattern is a good way to create software. it advocates some principles, such as separation of content and display, which may be better understood. However, if you want to isolate the components of models, views, and controllers, you may need to rethink your applications, especially the application architecture. If you are willing to accept MVC and have the ability to cope with the extra work and complexity it brings, MVC will put your software on a new level in terms of robustness, code reuse, and structure.

Java Web Application development involves several development methods that comply with the MVC design pattern.

1: Jsp + Servlet + JavaBean (EJB)

2: Jsp + JavaBean (Controller) + JavaBean (EJB) (Model)

3: TDK (Turbine, Velocity ...)

4: Xsp

5: Jsp + Struts + JavaBean (EJB)

Why? I remember the first interview with phper (php is another web development language that can be quickly used for me). people asked me about MVC. I only know that m is model and v is view, c is Controller,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.