控制器应该有多瘦,模型应该有多胖?

发布于 2024-10-19 22:12:25 字数 977 浏览 0 评论 0原文

控制器究竟应该有多薄?我理解将所有业务逻辑放入模型中,但其他事情呢?

例如,假设我正在编写一个博客网站,其中每个用户可以拥有多个帖子。目前,用户可以通过访问帖子控制器并运行创建操作来创建帖子。以下是当前会发生的情况的一些示例。

class Controller_Post extends Controller {
  function action_create() {
    if ( ! empty($_POST)) {
        $post = new Model_Post;
        $post->user_id = $this->logged_in_user->id;
        $post->values($_POST);
        if ( ! $post->create()) {
            echo 'Error';
        }
        else
        {
            echo 'Saved';
        }
    }
  }
}

我的问题是,什么会阻止我将上述逻辑放入用户模型中,就像这样。

class Model_User extends Model {
function create_post($post) {
            $post = Model::factory('post')->values($post);
    $post->user_id = $this->id;
    if ( ! $post->create()) {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
}

如果这样做的话,控制器会比我放的还要小。这对我来说更有意义,因为用户是创建帖子的人,所以我认为它应该位于用户模型中,而不是控制器中。

如果有帮助,我正在使用 Kohana 框架。

谢谢

Exactly how skinny should a controller be? I understand putting all of the business logic inside the models, but what about other things.

For example, say I was writing a blog site where each user can have multiple posts. Currently, the user would create posts by visiting the posts controller and running the create action. Here is a little sample of what would happen currently.

class Controller_Post extends Controller {
  function action_create() {
    if ( ! empty($_POST)) {
        $post = new Model_Post;
        $post->user_id = $this->logged_in_user->id;
        $post->values($_POST);
        if ( ! $post->create()) {
            echo 'Error';
        }
        else
        {
            echo 'Saved';
        }
    }
  }
}

My question is, what would stop me from putting the above logic in the user model, like so.

class Model_User extends Model {
function create_post($post) {
            $post = Model::factory('post')->values($post);
    $post->user_id = $this->id;
    if ( ! $post->create()) {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
}

If it were done this way, the controller would be even smaller than what I put. It makes more sense to me because the user is the one creating the post, so I think it should be in the user model as opposed to the controller.

If it helps, Im using the Kohana framework.

Thanks

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

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

发布评论

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

评论(1

↙厌世 2024-10-26 22:12:25

管制员应该指挥交通。模型适用于您的业务逻辑所在的位置,因此一般来说您的第二个示例将是“正确的 mvc”。

基本上,控制器应该做的是请求输入,告诉模型更改状态(它们自己更改实际状态),并确定要显示哪个视图(如果有)。

我有很多控制器,就像这样:

class Controller_Foobar extends Controller
{
    public function action_index() {}
}

如果它们需要处理 $_POST 输入,它们会获取该数据,并将其发送到模型,然后发送到视图。

将所有逻辑保留在模型中可以让您轻松地重用它,并且它更易于维护和测试。

Controllers should be directing traffic. Models are for where your business logic goes, so in general your second example would be "correct mvc".

Basically what a controller should be doing is requesting input, telling models to change state (they do the actual state change themselves), and determining which view to display (if any).

I have many controllers which simply are like so:

class Controller_Foobar extends Controller
{
    public function action_index() {}
}

And if they need to process $_POST input, they grab that data, and send it off to the model, then the view.

Keeping all that logic inside your models lets you easily reuse it, and it's more maintainable and testable.

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