什么,具体地,属于模型,视图和控制器? [英] What, specifically, belongs in a Model, a View, and a Controller?

查看:160
本文介绍了什么,具体地,属于模型,视图和控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习模型 - 视图 - 控制器范例(MVC),但我很困惑,因为一些教程与其他教程相矛盾。



我目前对此过程的理解如下所示:



路由器/调度程序/前端控制器:




  • 虽然在MVC名称中没有具体引用,但路由器仍然是非常重要的一部分。在这里,请求是从原始URL转换到特定的控制器。例如,路由请求www.StackUnderflow.com/question/123到应用程序的问题控制器



    • 型号:




      • 这是从某些存储器收集原始数据的地方源,如数据库或XML文件。该模型用作抽象层,将特定数据的控制器请求转换为(例如)SQL查询,并将查询结果转换为数据对象的标准格式。


      • 例如,在上述/ browse / all场景中:




        • 问题控制器会询问模型请给出问题123的数据。

        • 然后,模型会将其转换为SELECT * FROM Questions WHERE Id = 123;并将其放到数据库中

        • 数据库将向模型返回问题记录

        • 模型将记录下来,并将其翻译成一个问题数据对象

        • 模型然后要求做同样的事情SELECT * FROM Answers WHERE QuestionID = 123;并从结果集中创建一个数组的Answer对象,并将其添加到Question对象的答案成员变量中。

        • 将返回Question对象, em>问题控制器




      h2>


      • 这是应用程序的真正主力。除了向模型视图之间传送消息之外,控制器还负责授权(&D)和应用程序/商业逻辑编辑:根据答案,业务逻辑属于模型。


      • 在正在进行的示例中,控制器负责




        • 确保用户登录。

        • 从URL确定QuestionId。 li>
        • 确定要使用的视图。

        • 发送HTTP代码并根据需要重定向。





      查看:




      • 视图是应用程序中最简单的部分。它在基本应用程序中主要包含HTML模板。这些模板将具有占位符,可以从控制器的成员变量将数据插入到模板中:



      例如

       < html> 

      < head>
      < title>
      <?php $ question-> getTitle()?>
      < / title>
      < / head>

      < body>
      < h1> <?php $ question-> getQuestionText(); ?> < / H1>
      < h2>答案:< / h2>
      < div class =answerList>
      <?php formatAnswerList($ question-> getAnswers()); ?>
      < / div>
      < / body>

      < / html>




      • 该视图还将包含格式化数据以传送给用户的方法。例如,上面的 formatAnswerList()方法将从控制器获取一系列答案,并循环遍历它们,同时调用类似 include $ markupPath。 /formatAnswer.inc这将是一个只是答案容器的小模板。



      问题:




      • MVC的这个观点是否基本准确?

      • 如果没有,请仔细说明哪些组件是错误地放置在哪里,以及应该如何正确地与其他组件进行交互(如果有的话)。

      • 有多少类用于描述?在我的例子中,有四个对象 - 一个用于MVC的三个组件,一个简单地存储相关数据进行传输。这是正常的,还是应该结合一些。如果是这样,那么哪个?


      解决方案

      并且模型不够。该模型是理想的业务逻辑所在。控制器实际上只是一个用户站点的接口,路由控制需要去。看看以前关于这个主题的讨论:



      了解MVC:Fat的概念是什么在模型上,瘦在控制器上?


      I've been learning about the Model-View-Controller paradigm ("MVC"), but I'm quite confused since some tutorials contradict other tutorials.

      My current understanding of the process looks something like this:

      Router / Dispatcher / Front Controller:

      • Though not specifically referenced in the "MVC" name, the Router is still a very important part. It is here that requests are translated from raw URLs to a specific controller. For example, routing a request for www.StackUnderflow.com/question/123 to the "Question" Controller of the application.

      Model:

      • This is where raw data is collected from some storage source, such as a database or XML file. The model serves as an abstraction layer, translating the Controller's request for specific data into (for example) an SQL query, and translating the query results into a standard format like a data object.

      • For example, in the /browse/all scenario stated above:

        • The "Question" Controller would ask the Model "Please give the data for question 123."
        • The Model would then translate that into "SELECT * FROM Questions WHERE Id = 123;" and punt it to the database
        • The database would return a "Question" record to the Model.
        • The Model would take the record, and translate it into a Question data object
        • The Model then asks does the same thing "SELECT * FROM Answers WHERE QuestionID = 123;" and creates an array of Answer objects from the resultset, and adds that to the Question object's answers member variable.
        • The Model would return the Question object to the "Question" Controller.

      Controller:

      • This is the real workhorse of the application. In addition to relaying messages back and forth to the Model and the View, the Controller is also responsible for things like Authorization and application/"business" logicEdit: Per answer, business logic belongs in the Model.

      • In the ongoing example, the Controller wold be responsible for:

        • Ensuring the user is logged in.
        • Determining the QuestionId from the URL.
        • Determining which View to use.
        • Sending HTTP codes and redirecting if needed.
        • Asking the Model for data, and store needed data in member variables.

      View:

      • By and large, the View is the simplest part of the application. It mostly consists, in a basic application, of HTML templates. These templates would have placeholders to insert data into the template from the Controller's member variables:

      e.g.

      <html>
      
        <head>
          <title>
            <?php $question->getTitle() ?>
          </title>
        </head>
      
        <body>
          <h1> <?php $question->getQuestionText(); ?> </h1>
          <h2> Answers: </h2>
          <div class="answerList"> 
            <?php formatAnswerList($question->getAnswers()); ?> 
          </div>
        </body>
      
      </html>
      

      • The View would also contain methods to format data for delivery to the user. For example, the formatAnswerList() method above would take an array of answers, taken from the Controller, and loop through them while calling something like include $markupPath . "/formatAnswer.inc" which would be a small template of just an answer container.

      Questions:

      • Is this view of MVC fundamentally accurate?
      • If not, please carefully explain which components are misplaced, where they should actually go, and how they should properly interact with the other components (if at all).
      • How many classes are used to describe this? In my example there are four objects - one each for the three components of MVC and one that simply stores related data for transmission. Is this normal, or should some be combined. If so, which ones?

      解决方案

      I think this description puts too much weight on the controller and not enough on the model. The model is ideally where the business logic resides. The controller is really just an interface for the user of the site, routing control where it needs to go. Take a look at a previous discussion on the topic:

      Understanding MVC: Whats the concept of "Fat" on models, "Skinny" on controllers?

      这篇关于什么,具体地,属于模型,视图和控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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