MVC中控制器的作用是什么? [英] What is the job of controller in MVC?

查看:1272
本文介绍了MVC中控制器的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习MVC架构.但是我不明白为什么需要控制器.有关我的模型和视图,请参见下面的代码.

I am trying to learn the MVC architecture. But I am not able to understand why you need a controller. See the below code for my model and view.

model.php连接到数据库并检索帖子. view.php只会显示该帖子.

model.php connects to the database and retrieves the post. view.php will just display the post.

model.php

model.php

<?php
  $db = mysql_connect("somehostname", "someuser", constant("somepassword"));
  mysql_select_db("somedatabase", $db);

  $result = mysql_query("SELECT post FROM posts WHERE postid='" . $_POST['id'] . "'");
  $row = mysql_fetch_array($result);

  $post = $row["post"];

  mysql_close($db);
?>

view.php

<?php
  require "model.php";
  echo $post;
?>

我将浏览器位置设置为 http://whateverhost/view.php?id = 5

I set my browser location to http://whateverhost/view.php?id=5

此加载ID为5的帖子.我在这里不需要控制器.所以我很困惑,为什么您需要控制器?

This loads post with the id 5. I did not require a controller here. So I am confused why do you need a controller?

注意:请参考以上示例进行说明.我不是一个编程狂,学习CakePHP之类的东西对我来说是压倒性的.

Note: Please explain with reference to the above example. I am not a programming geek and learning things like CakePHP, etc. is overwhelming for me.

如果您可以将controller.php添加到上面的代码中,那就太好了.这将有助于我理解控制器的角色以及控制器与模型和视图的通信方式.

It would be great if you can add controller.php to the above code. That would help me in understanding the role of a controller and how it communicates with model and views.

推荐答案

您不需要控制器,因为您的示例很简单. 实际案例中的一个示例:

You don't need a controller because your example is trivial. An example from a real case scenario:

假设您有一个CAD应用程序. CAD应用程序安排为MVC.你有:

Suppose you have a CAD application. The CAD application is arranged as MVC. You have:

  • 一个负责绘制当前模型并提供可点击实体的视图.
  • 一个模型,用于保持当前数据的显示状态
  • 一个控制器,其作用是从视图中获取事件,并将其转换为适当的实体,以便修改模型.

例如,用户单击一个正方形并将其删除.控制器将从视图中接收事件,创建代表命令的对象(通过命令模式),将其添加到具有撤消功能的队列中并执行命令.该命令将修改模型,但是将视图事件转换为修改模型的复杂机制的责任在于控制器.

For example, the user clicks on a square and deletes it. The controller will receive the event from the view, creates an object representing a command (via the Command pattern), add it into a queue for undo capability and executes the command. The command will then modify the model, but the responsibility of translating view events into the complex machinery that modifies the model is under the responsibility of the controller.

您当然可以说,为什么视图不创建Command对象呢?好吧,没有人禁止您这样做,但是最终您将演示逻辑与操作逻辑混为一谈.这与良好的设计背道而驰,尽管对于大多数琐碎的案例,您可以接受这种设计.例如,如果您的CAD应用程序允许您既以3D表示形式又以实体列表形式显示对象列表,并且可以从两者中删除,则您清楚地看到这两个视图都实现了相同的逻辑来处理命令模式(错误的设计),或者它们只是将相同的消息传递到通用控制器(良好的设计,MVC).

Of course you could say, why isn't the view creating Command objects then? well, nobody forbids you that, but you would end up having presentation logic mingled with operational logic. This goes against good design, although for the most trivial cases, you could live with this design. For example, if your CAD application allows you to display the list of objects both as a 3D representation and as a list of entities, and you could delete from both, you clearly see that either the two views both implement the same logic to handle the command pattern (bad design), or they just channel the same message to a common controller (good design, the MVC).

这篇关于MVC中控制器的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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