Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the meaning and division of responsibilities of MVC architecture

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the meaning and division of responsibilities of MVC architecture". In daily operation, I believe that many people have doubts about the meaning of MVC architecture and division of responsibilities. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what is the meaning of MVC framework and division of responsibilities?" Next, please follow the editor to study!

1 what on earth is MVC

Model-View-Controller (MVC) is a design framework (design pattern).

The goal of MVC is to separate business logic from user interface considerations.

In this way, developers can more easily change each part without affecting the others.

In MVC

Model represents data and business rules

View contains user interface elements, such as text, forms, etc.

Controller manages the communication between the model and the view.

MVC is implemented in various programming languages, such as J2EE application development

View may be implemented by jsp; Controller is a servlet that is now generally implemented in Struts; and Model is implemented by an entity Bean.

What's the problem with me?

Yii Framework is a popular PHP framework that draws on Ruby on Rails's concept of ActiveRecord (AR).

Every table in the database can use the AR class to easily add, delete, modify and query.

It treats AR as a Model and recommends putting it in a directory called models.

So, after automatically generating the corresponding AR for the table, I took it for granted that I already had the Model layer.

In fact, AR is just a DAO (data access layer), not a Model layer.

Our business is almost entirely in Controller: making all kinds of logical judgments and calculations on the forms submitted by users, instantiating AR to store data.

Because there will be multiple action in a Controller, each action has such a business process.

In the end, I found that my Controller code had exceeded 1000 lines.

Suddenly one day, leader said, our system should open API to existing old system calls and give third-party interfaces.

The third party only needs to give a parameter, and the system gives a result value, which does not care about the business processing.

The bad thing is that Controller has implemented those businesses, but it accepts form submissions, so how can it also accept SOAP xml documents?

Controller, like condoms, should be as thin as possible.

Its job should be to accept input from the user and immediately forward it to another class for processing.

In this way, Controller is only responsible for providing different interfaces, so that we can separate the business logic, and the separated business can be easily reused.

Who will handle this part of the separated business? The answer should be Model.

3 duties of View

The View part is relatively clear, which is responsible for display.

Everything that has nothing to do with the display interface should not appear in view.

Therefore, there should be no complex judgment statements and complex operations in View.

There can be simple loop statements and formatted statements. For example, the text list on the front page of a blog is a loop.

For the Web application of PHP, HTML is the main content of View.

View should never call the write method of Model.

That is, View only reads data from Model, but does not overwrite Model.

So we say that View and Model have nothing to do with each other.

Also, instead of accessing $_ GET and $_ POST directly in View, it should be passed to View by Controller.

In addition, View generally does not have anything to prepare for data processing, such as querying the database.

These are typically placed in Controller and passed to the view in the form of variables.

In other words, the data to be used in the view is a variable.

4 duties of Model

For Model, the most important thing is to save and output information.

For example, the Post class must have a title property to hold the title of the blog post, and there must be a delete operation, which is the content of the Model.

Data, behavior and method are the main contents of Model.

In practice, Model has the largest amount of code in MVC.

Model is the most complex part of the logic, because the business logic of the application is also represented here.

Note that Model is distinguished from Controller.

Model is the logic of dealing with business, and Controller is simply coordinating the relationship between Model and View.

As long as it is related to business, it should be put in the Model.

Data parity, public constants and variables should all be placed in the model layer

In other words, properties or methods that are likely to be reused should be placed in the model layer, defined at once, and used everywhere.

Model should not access request, session, and other environmental data, which should be injected by Controller.

A good design should be fat Model and thin Controller.

5 duties of Controller

For Controller, the main task is to respond to the user's request and decide which view to use and what data to prepare for display.

Therefore, the access code for request should be placed in Controller, such as $_ GET, $_ POST, and so on.

Controller should be limited to getting data requested by users, and there should be no manipulation or preprocessing of the data, which should be placed in the Model.

For the data write operation, call the method of the Model class to complete.

For the response to the user's request, the view rendering is invoked.

In addition, there is generally no other presentation layer things such as HTML code, which should belong to View.

At this point, the study of "what is the meaning of the MVC architecture and the division of responsibilities" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report