duanmi8349 2014-08-21 10:53
浏览 34
已采纳

为什么控制器需要访问Web PHP MVC中的模型?

Why wouldn't MVC not work just fine if the controller would only have access to the view (diagram #2 in the image below) and the view would have access to the model?

On most sites (some topics on Stack Overflow included), I have read that it's should be like shown in the left diagram (diagram #1 in the image below).

I have added an image, just so that you can understand my question better (don't mind that this is a UML diagram, look at it just as an normal diagram).

enter image description here

NOTE: Of course there is an dataMapper to which one the model has access and an init and frontController above the controller, etc. But I didn't add them to the image just to keep it as simple as possible.

  • 写回答

1条回答 默认 最新

  • dph58509 2014-08-21 10:58
    关注

    If it's about simply displaying data, then yes, the schematic should be that the controller invokes a view and the view gets data from the model to visualise its state.

    However, oftentimes the controller triggers change in the model's state. E.g. a user asks to be logged in, a new entry is posted, an old entry is deleted, or any other kind of change that's triggered by incoming requests. The controller propagates those "write requests" to the appropriate model method and decides on what should be done further based on the outcome. The view is "read only", it's not its job to handle such things.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器