在“经典”网络 MVC 中——如果我错了请纠正我:
- Controller 将从“用户”(浏览器、控制台等)接收到的请求数据传递给模型层(由领域对象、映射器、存储库、服务等组成),
- 模型层对其进行处理并返回一些结果数据,
- View - 作为专门的类 - 处理结果数据并将其发送/显示给“用户”。
我想问:
- Controller 是否创建 View ?
- 或者 Controller 是否将 View 作为依赖项接收?
- 或者 Controller 和 View 是否在前端 Controller 级别完全分开创建(比如说在 index.php 中)?
谢谢。
最佳答案
您对 MVC 的定义通常是正确的,这里是您问题的答案:
Controllers are not responsible for rendering the interface, nor for presentation logic. Controllers do not display anything. Instead, each controller's method deals with different user's request. It extracts the data from said request and passes it to model layer and the associated view.
Decisions about what and how to display are in purview of views. Views contain the presentation logic in MVC pattern. In the context of web applications, views create the response. They can compose a from from multiple templates or just send a single HTTP header.
Controllers can signal the associated view by passing some specific values of the request to that view, but most of the decisions in the view are based on information that the view requested from different services in the model layer.
A Controller's methods are based on what type of requests a user can send. For example in a authentication form it might be: GET /login and/or POST /login.
来源:Controllers , tereško
经典正确的MVC类结构:
简单定义:
Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).
View. The view manages the display of information.
Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.
其他资源: (只有有用的)
外部
- MVC Explanatory [计算机科学设计模式]
- Creating a Custom Controller and View in CodeIgniter [视觉示例]
- Codeproject 的定义 MVC:easy | extended
内部
关于model-view-controller - 在 "classical"web MVC 中, View 是如何创建的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149986/