Home  >  Article  >  Backend Development  >  A problem between the control layer calling the model and the view layer calling the model?

A problem between the control layer calling the model and the view layer calling the model?

WBOY
WBOYOriginal
2016-09-21 14:12:581124browse

In a frame, I want to outputdata in the view layer:

The first method is to assemble all the data in the controller layer. When it is time to call the model, when rendering the view layer template, there is no need to read the database , just use the php function to output it directly. .

Second, in the controller layer, simply process the necessary data, and then when the view layer template is rendering, read the database according to the situation;

Which of these two situations is better?

---------------------Supplement----------------------
My opinion : I think it doesn’t matter, because introducing a model in a controller method is equivalent to loading a file, then reading the data in this method, and finally introducing the template file, then It's okay for me to read the database in this template file, but the former increases the readability and simplicity of the template code. In a practical sense, there is no big difference. I wonder if this is the case?

---------------------Supplement----------------------
Those who know MVC Features, but I saw that some seniors directly performed database queryoperations in the template, so I had such a question, please help~

Reply content:

In a frame, I want to outputdata in the view layer:

The first method is to assemble all the data in the controller layer. When it is time to call the model, when rendering the view layer template, there is no need to read the database , just use the php function to output it directly. .

Second, in the controller layer, simply process the necessary data, and then when the view layer template is rendering, read the database according to the situation;

Which of these two situations is better?

---------------------Supplement----------------------
My opinion : I think it doesn’t matter, because introducing a model in a controller method is equivalent to loading a file, then reading the data in this method, and finally introducing the template file, then It's okay for me to read the database in this template file, but the former increases the readability and simplicity of the template code. In a practical sense, there is no big difference. I wonder if this is the case?

---------------------Supplement----------------------
Those who know MVC Features, but I saw that some seniors did database queryoperations directly in the template, so I had this question, please help~

The first type, single responsibility

When I first started writing code, I wrote all database queries into templates to save effort and solve all problems with one method. It will not be until a month or two after the project has been running that the effort saved in the early stage for the increasingly large code blocks will have to be doubled at this time, and the further the project expands, the more time it will cost.

Now I use the first one

How should I put it? In fact, each has its own advantages.
Of course, whether from the perspective of specifications or project development, it is recommended to use the first one as much as possible.

If it is a small project developed by individuals and there are not too many functions and later expansions, it is faster to use the second method. Use it anytime, take it anytime

The first one is better! The first type also depends on whether the business logic is placed in M ​​or C. This also varies from person to person.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn