用户输入是否进入控制器或模型?

发布于 2024-11-05 16:45:05 字数 620 浏览 3 评论 0原文

现在我已经拆分了模型,但我的控制器和视图仍然组合在一个 12k 行文件中。我一直在寻求为此创建一个真正的 MVC 系统,拆分视图,但是在寻找要拆分的内容时,我注意到我的控制器正在执行大量可能属于模型的工作。

例如,假设我有……

if (isset($_POST['write'])) {
    $obj = $objManager->get($_POST['id']);
    $obj->setFoo($_POST['foo'])
        ->setBar($_POST['bar']);
    $objManager->write($obj);
    echo ... 
}

所以 echo 之后的内容,我们知道会进入视图模板。经理是我的榜样。所以我的问题是,我是否要从 $_POST 进行所有读取并在控制器中设置数据?或者我是否以某种方式将其放入模型中,就像这样......

if (isset($_POST['write'])) {
    $objManager->update($_POST);
    echo ...
}

其中 update() 执行基本相同的操作,设置变量并保存事物。

Right now I have my model split out, but my controller and views are still combined in a 12k line file. I've been looking to create a true MVC system for this, splitting out the views, but while looking for stuff to split out, I've noticed my controller is doing a lot of work that might belong in a model.

For example, let's say I have...

if (isset($_POST['write'])) {
    $obj = $objManager->get($_POST['id']);
    $obj->setFoo($_POST['foo'])
        ->setBar($_POST['bar']);
    $objManager->write($obj);
    echo ... 
}

... so the stuff after echo, we know goes into a view template. The manager is my model. So my question is, do I do all the reading from $_POST and setting up the data in the controller? Or do I somehow put that into the model, like so...

if (isset($_POST['write'])) {
    $objManager->update($_POST);
    echo ...
}

... where update() does basically the same thing, sets the variables and saves the thinger.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2024-11-12 16:45:05

你的第一个例子对我来说看起来是更好的解决方案。它正确地将接受用户输入的任务与访问模型中的数据的任务分开。换句话说,模型不关心输入来自 POST 参数这一事实。

在第二个示例中,您将模型与控制器紧密耦合,因为模型期望将 POST 参数列表传递给它以便更新对象。模型不必关心“id”、“foo”和“bar”变量来自何处。

第二个例子看起来更优雅一点,但是对单元测试不太友好。为了对其进行单元测试,您必须向其传递一个关联数组,其键与 POST 参数的名称匹配。这在 PHP 中并不是什么大问题,因为一切都是动态类型的,但这是您必须担心的另一件事。

Your first example looks like the better solution to me. It properly separates the tasks of accepting user input from accessing the data in your model. In other words, the model doesn't care about the fact that the input is coming from POST parameters.

In the second example, you are tightly coupling the model with the controller because the model expects that a list of POST parameters be passed to it in order to update the object. The model shouldn't have to care about where the "id", "foo", and "bar" variables are coming from.

The second example looks a little more elegant, but it is not as friendly to unit testing. In order to unit test it, you would have to pass it an associative array, whose keys match the names of the POST parameters. This is not as big of a deal in PHP, since everything is dynamically typed, but it's one extra thing you have to worry about.

暮年 2024-11-12 16:45:05

让控制器处理所有用户输入并远离模型内的 $_POST 变量,而是让控制器填充模型是相当常见的。要获得有关 MVC 方法的更多观点,您可以看看其他框架是如何做到这一点的,例如 Zend 框架

It's fairly common to have the controller take care of all the user input, and stay out of the $_POST variable inside the model, and rather have the controller populate the model. To get more perspectives on the MVC approach, you could have a look at how other frameworks do it, E.G. the Zend Framework

满栀 2024-11-12 16:45:05

思考 MVC 的一个好方法是问自己,“如果我在视图层中更改 X,我需要在控制器或模型层中更改哪些内容?”

理想情况下,模型层应该能够独立于视图层而存在,因为它充当应用程序的核心 API。对视图的更改不应需要在模型中进行返工。

控制器接收外部输入(例如来自用户的输入),调用模型层来处理它,然后确定提供哪个视图作为响应。

在您的示例中, $_POST 包含原始输入,并且 $_POST 数组中的键由它们在大多数 HTML 视图中的编码方式指定。您不应该期望您的模型知道 $_POST 中的有效键是什么,或者这些值是否需要清理、转换等。将这项工作留给控制器,控制器应该保证它传递给模型的值能够满足要求模型层类/函数期望的条件。

A good way to think about MVC is to ask yourself, "If I changed X in the view layer what would I have to change in the Controller or the Model layers?"

Ideally, the Model layer should be able to exist independent of the View layer, since it functions as the core API of your application. Changes to a view should not require rework in the model.

The Controller is what takes external input (like from the user), calls into the Model layer to process it, and then determines which view to provide as a response.

In your example, $_POST contains the raw input, and the keys in the $_POST array are specified by how they are encoded in your mostly HTML views. You should not expect your model to know what the valid keys are in $_POST or whether the values need to be sanitized, transformed, etc. Leave that job to the controller, which should guarantee that the values it hands off to the model will meet the conditions that the Model layer classes/functions expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文