适用于简单应用的MVC [英] MVC for simple app

查看:82
本文介绍了适用于简单应用的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个简单的付款计算器,我已经通过html前端和php后端正常工作. html通过AJAX调用将用户输入作为变量传递给PHP,PHP将结果返回到html页面.现在,它已按预期运行,我正试图弄清楚如何将其转换为MVC模式.以我的理解,html是视图组件,PHP是模型,它包含所有用于计算的逻辑,但是我真的不确定控制器中会发生什么?如果控制器的工作是在视图和模型之间进行协调,那么我应该将输入数据传递给控制器",然后将其传递给php,然后通过相同的路线将结果传递回去吗?我原则上理解这种模式,但是在这种情况下,这似乎是多余的.我一直在研究几个MVC框架(.NET,Laravel),但确实希望获得更基本的了解,而无需所有内容.任何建议表示赞赏!

I have been working on a simple payment calculator, which I have working correctly via a html front end and a php back end. The html passes user inputs as variables to the PHP via AJAX calls and the PHP returns a result to the html page. Now that I have it functioning as expected, I'm trying to figure out exactly how I might convert this to an MVC pattern. In my understanding, the html is the view component, the PHP, which contains all the logic for calculation, is the model, but I'm really unsure what would go in the controller? If the controller's job is to co-ordinate between the view and the model, then should I be passing my input data to the "controller" which then passes it to the php and then pass the result back via the same route? I understand this pattern in principle but this seems extraneous in this case. I have been looking into several MVC frameworks (.NET, Laravel) but really want to get a more fundamental understanding without having everything scaffolded. Any advice appreciated!

推荐答案

MVC架构模式实际上不适用于简单应用程序".增加所有这些复杂性往往是不值得的.这是因为从本质上讲,"MVC"只是用于各个层的一组约束,当大型代码库变得难以包含时,可以添加这些约束.

MVC architectural pattern really is not intended for "simple apps". It tends not to be worth the effort of adding all that complexity. That's because in essence "MVC" is just a set of constraints for various layers, which you add when a large codebase becomes hard to contain.

这不是魔术般的尘埃,可以撒在自己的项目上.

It's not a type of magic fairy dust, that you can sprinkle on your project.

一个代表您的理解" ..很好..这些视图最终并没有以"html"结尾.在此处上有更长的文章,但TL; DR版本是:模板实际上是处理html和视图的方法是管理/组装/填充模板的对象.

A for your "understanding" .. well .. the views really don't end up as "html". There is a bit longer post on it here, but the TL;DR version is: templates are what really handle html and views are what manage/assemble/populate the templates.

关于您的主要问题:控制器的责任是更改模型层的状态.而已.

As to your main question: the responsibility of controllers are to alter the state of model layer. That's it.

它与脚手架无关(脚手架是一种快速发展的工具),它不协调任何事情.就像我上面说的:MVC是关于约束的.在UI层中,控制器使用用户的输入来更改模型的状态,而视图则代表当前模型的状态.

It has nothing to do with scaffolding (which is a rapid-development tool) and it does not coordinate anything. As I said above: MVC is about the constraints. In the UI layer the controllers use user's input to alter the model's state and the views make a representation of the current model's state.

PS .路由"实际上与MVC无关,因为它本质上是HTTP抽象中的一个细节.您只需使用它来决定要戳" UI层的哪一部分.

P.S. "Routing" is actually unrelated to MVC, because it essentially is a detail in the HTTP abstraction. You only use it to decide, which part of UI layer to "poke at".

这篇关于适用于简单应用的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆