当控制器真正应该放在模型中时,最常见的东西是什么,反之亦然?

发布于 2024-10-31 12:03:49 字数 121 浏览 0 评论 0原文

我仍在尝试将我的思想集中在 MVC 模式上,以及到底什么应该放置在控制器中,什么应该放置在模型中。我读过控制器主要包含应用程序逻辑,模型应该包含所有业务逻辑。有时很难区分两者。你到底在哪里划清界限?哪些类型的行动属于两者的边缘?

I'm still trying to wrap my mind around the MVC pattern and exactly what is supposed to be placed in Controllers, and what should be placed in Models. I've read that controllers mostly contain application logic, and models should have all the business logic. It's sometimes hard to differentiate between the two. Where exactly do you draw the line? What types of actions are on the fringe of both?

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

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

发布评论

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

评论(3

梦里兽 2024-11-07 12:03:49

MVC 是一个含义相当丰富的术语,它对不同的作者有不同的含义。

当它在 Smalltalk 中引入时,控制器用于用户输入,视图用于输出,模型用于与问题空间相关的状态。

苹果还在其 iOS 和 Mac OS 文档中使用了术语 MVC,但模型基本上用于数据库访问/问题空间,视图用于输入和输出,控制器用于将模型和视图连接在一起。

MVC 也被其他人使用,并且这些术语在每种情况下的含义略有不同,其中控制器是它们之间变化最大的。所有这些都意味着回答你的问题是相当困难的。

但从抽象的角度来看,模型应该由领域专家能够理解的内容组成。您谈论的线条图主要与用户输入的验证有关,用户输入通常放入控制器中,因为对无效输入的响应通常取决于应用程序,并且通常需要在将错误输入发送到分布式模型之前将其清除。系统。

希望这会有所帮助,但问题本身很模糊,所以答案也是如此。

MVC is a rather loaded term, it means different things to different authors.

When it was introduced in Smalltalk, controllers were for user input, views were for output, and models were for state related to the problem space.

Apple also uses the term MVC in its documentation for iOS and Mac OS, but there the model is for basically database access/problem space, the view is for both input and output and controllers are for wiring Models and Views together.

MVC is used by others as well, and again the terms mean slightly different things in each case, with the controller being the most variable between them. All this means that answering your question is quite difficult.

In the abstract though, the model should consist of that which a domain expert would understand. The line drawing you talk about has mostly to do with validation of user input which is often put in the controller because the response to invalid input is usually application dependent and often bad input needs to be weeded out before sent off to the model in a distributed system.

Hope this helps somewhat, but the question itself is vague, so the answer will be too.

咿呀咿呀哟 2024-11-07 12:03:49

数据访问(例如数据库调用)应该在模型中。

用户输入应由控制器处理(可以将其移交给模型)。

Data access (e.g. Database calls) should be in the Model.

User input should be handled by the Controller (where it may be handed off to a Model).

奢欲 2024-11-07 12:03:49

模型控制器事情的好文章

这是关于 ZF mvc 实现我认为最好的方法是将几乎所有内容保留在模型中而不是控制器中。控制器仅用于将用户输入映射到适当的模型方法。
我当前的模型有几个层:静态服务定位器、服务(有点业务逻辑层)、模型和映射器。

虽然我目前的实现在 OOP 方面很糟糕,所以我不会向您提供更多细节。

我的应用程序中的快速示例操作,简单的目录模块:

public function addAction()
{
    $service = Xrks_Service::getInstance()->getService('catalog', true);
    if(!$service->checkAcl('addItem')) {
       throw new Exception('You have no rights to view this page', 403);
    }
    $item = $service->getNewItem();
    $form = $service->getEditForm($item); //here form created and filled with default values from model
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
        $service->populateModel($item, $form); //form->model mapper hidden behind service interface
        $item->save();
        $this->_helper->getHelper('Redirector')->gotoRouteAndExit(array('action' => 'edit', 'id' => $item->getId()), 'catalog-item');
    }
    $this->view->form = $form;

}

This is good post on model-controller thing

as for ZF mvc implementation i think best approach is to keep almost everything in you model rather than controller. Controller is just for mapping user input to appropriate model methods.
My current model have several layers: static service locator, services(kinda business logic layer), models, and mappers.

Though my current implementation is bad in terms of OOP so i won't give you more details.

And quick example action from my app, simple catalog module:

public function addAction()
{
    $service = Xrks_Service::getInstance()->getService('catalog', true);
    if(!$service->checkAcl('addItem')) {
       throw new Exception('You have no rights to view this page', 403);
    }
    $item = $service->getNewItem();
    $form = $service->getEditForm($item); //here form created and filled with default values from model
    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
        $service->populateModel($item, $form); //form->model mapper hidden behind service interface
        $item->save();
        $this->_helper->getHelper('Redirector')->gotoRouteAndExit(array('action' => 'edit', 'id' => $item->getId()), 'catalog-item');
    }
    $this->view->form = $form;

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