控制器与模型 - 需要解释

发布于 2024-10-11 13:16:15 字数 711 浏览 3 评论 0原文

我正处于“学习 MVC”之路的开始。基本上,我对面向对象编程没有什么大问题,但是有一个技术方面需要澄清。看来我的理论还不够完善。

目前,我正在使用 KohanaPHP 框架,版本 3。

示例情况: 我有一个网站,用户可以在其中提交文章。

所以我有以下结构:

classes/
    /controllers/
        article.php
    /models/
        articles.php

到目前为止一切顺利。我对扩展 Kohana_Model 的模型没有任何问题,但是我不确定我是否使用使用 ORM 的正确方法模型。

基本上,当使用扩展 Kohana_Model 的模型时,我会将所有逻辑操作放入模型中。我应该对使用 ORM 的模型做同样的事情吗?在网络上的许多示例中,我看到控制器对数据库中的用户输入/数据执行逻辑操作,我认为这是不正确的。

假设我需要从数据库中获取几行,因此我在模型中创建正确的方法并返回对象。我认为这是正确的,不是吗?

基本上,对用户输入/数据的所有操作(从数据库中选择、插入数据库、验证)我都放在模型中。这就是我理解MVC设计模式的方式。模型应该关心所有“机械”操作,控制器只是模型/视图之间的“桥梁”,它是一个“前端”引擎。

这是正确的做法吗?

我知道对于更高级的用户来说这可能是一个愚蠢的问题,但我只想学习好的实践。如果有人能提供一些澄清,我会很高兴。

I'm on the beginning of my "Learn MVC" way. Basically, I don't have big problems with object-oriented programming however there's one technical aspect that needs clarification. It seems that my theory isn't quite good enough.

Currently, I'm using KohanaPHP framework, version 3.

Example situation:
I have a website, where user can submit an article.

So I have following structure:

classes/
    /controllers/
        article.php
    /models/
        articles.php

So far so good. I don't have problems with models that extends Kohana_Model however I'm not sure if I'm using the correct way models that are using ORM.

Basically when using models extending Kohana_Model I'll put all logical operations in model. Should I do the same for models using ORM? In many example over the Net I saw controllers that was performing logical operations on user input/data from database which is incorrect in my opinion.

Let's say I need to get few rows from database so I create proper method in model and returning the object. I think it's correct, isn't it?

Basically, all operations on user input/data (select from db, insert into db, validation) I put in the models. That's the way I understand MVC design pattern. Models should take care about all "mechanic" operations and controller is only a "bridge" between models/views and it's a "front" engine.

Is it a correct approach?

I know that might be a silly question for more advanced users however I want to learn only good practices. If anyone could deliver some clarification, I'll be delighted.

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

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

发布评论

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

评论(4

゛时过境迁 2024-10-18 13:16:15

简而言之,您的模型对数据执行所有操作(无论是传入、传出、数据库、文件......数据),并且您的视图应该负责显示数据。控制器应该调用必要的模型方法来准备将数据传递到视图。控制器不应对数据执行任何更改,但应对其进行测试以正确完成必要的操作。

希望我说得足够清楚,如果这对您来说还不清楚,请告诉我。

In short terms, your model performs all operations on the data (be it incoming, outgoing, database, files ... data), and your view should take care of displaying the data. The controller should call the necessary model methods to get the data ready to be passed to the view. The controller shouldn't perform any changes to the data, but it should test it to get the necessary action done properly.

Hope I made it clear enough, let me know if this doesn't clear things for you.

青春有你 2024-10-18 13:16:15

我没有使用过 KohanaPHP,但它看起来像是一个“受 Rails 启发”的框架。
在 Rails 世界中,通常认为最佳实践是使用瘦控制器和胖模型。

一些背景可以在 jamis Buck 的这篇旧文章中找到 http:// /weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model 或在这一篇中有关 cakephp http://gluei.com/blog/view/cakephp-best-practices-fat-models-and-skinny-controllers

I haven't worked with KohanaPHP but it looks like a 'rails-inspired' Framework.
In the rails world it is generally considered best practice to have skinny controllers and fat models.

some background can be found in this old article by jamis buck http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model or in this one concerning cakephp http://gluei.com/blog/view/cakephp-best-practices-fat-models-and-skinny-controllers

迷你仙 2024-10-18 13:16:15

将逻辑与数据分离的想法是数据不包含逻辑,因此在模型中您应该只真正清理数据。

举个例子:

public class Articles extends MasterModelLayer
{
    public function create($title,$text,$user_id = false)
    {
        if(!$user_id)
        {
             $user_id = get_guest_id();
        }
        //Insert
    }
}

模型中的逻辑似乎是合法的,但从现在开始,您的应用程序必须能够允许访客发布文章,否则它有缺陷,您需要编辑模型,这将影响您网站的其他应用程序/区域

采取这种情况:

您有 2 个站点

  • ComputerArticles.com
  • CookingArticles.com

现在,这 2 个域指向同一台服务器,但在 kohona 中是一个单独的应用程序,不要在模型中放置任何域逻辑,您就可以在所有域中使用确切的示例模型。域。

您的模型方法应该如下所示:

public class Articles extends MasterModelLayer
{
    public function create($title,$text,$user_id)
    {
        $this->compile("articles",array($title,$text,$user_id))->insert();
    }
}

在您的控制器中,您应该放置所有逻辑,因为逻辑将取决于域。

将您的模型视为一个 API,其中您有多个站点使用相同的 API 但在不同的逻辑下。

希望这有帮助。

The idea of seperating logic from data is that data does not contain logic, so in your models you should only really be sanitizing data.

Take this example:

public class Articles extends MasterModelLayer
{
    public function create($title,$text,$user_id = false)
    {
        if(!$user_id)
        {
             $user_id = get_guest_id();
        }
        //Insert
    }
}

Seems legitimate logic in the models, but from now on your application must be able to allow guests to post articles, or its flawed and you need to edit the models, which then will floor your other applications / areas of your site

Take this scenario:

You have 2 sites

  • ComputerArticles.com
  • CookingArticles.com

Now these 2 domains point to the same server but a seperate application in kohona, be not placing any domain logic within your models your able to use the exact sample models accross all your domains.

Your Model Methods should look like so:

public class Articles extends MasterModelLayer
{
    public function create($title,$text,$user_id)
    {
        $this->compile("articles",array($title,$text,$user_id))->insert();
    }
}

And within your controllers you should place all your logic as the logic will depend on the domain.

Think of your Models as an API, where you have multiple sites using the same API But under different logic.

Hope this helps.

一紙繁鸢 2024-10-18 13:16:15

以下是术语的基本外行定义 -
视图:呈现给用户的屏幕
控制器:接收请求、确定谁处理请求并适当地委派它们的引擎/框架。
模型:这基本上告诉您在该屏幕上完成某某操作后要显示什么屏幕。将其视为一个(有向)图。从节点出现的边是动作,它们连接到的节点是基于这些动作的下一个屏幕。

因此,模型基本上包括用户在屏幕上执行的操作及其操作处理程序。控制器只是为特定的用户操作调用相应的操作处理程序,并且操作处理程序负责对传入的请求执行一些明智的操作。

现在你的问题。业务逻辑去哪儿了?
嗯,它是动作处理程序。或者它被抽象到人们喜欢称之为的某个地方——业务层。但无论如何,它是由动作处理程序本身触发的。

因此,从技术上讲,业务逻辑是操作的一部分,而操作本身也是模型的一部分。
如果您这样认为,这是有道理的:控制器处理用户交互并基于模型(操作+业务)呈现视图。

** 已更正拼写错误。

Here are the basic layman definitions of the terms -
Views: The screens presented to the users
Controller: An engine/framework that takes in requests, determines who handles it and delegates them appropriately.
Model: This basically tells you what screen to show after so and so action is done on this screen. Think of it as a (directed) graph. The edges emerging from a node are actions and the the nodes they connect to are next screens based on those actions.

So a model basically includes the actions that user does on the screens and their action-handlers. The controller simply calls in a corresponding action-handler for a particular user action and the action-handler is responsible to do something sensible with the incoming request.

Now your question. Where does business logic goes?
Well it is the action handler. Or it is abstracted somewhere people like to call - business layer. But anyways it is triggered off from from the action-handlers itself.

So, technically, business logic is part of actions which themselves are part of the Model.
This makes sense if you think it this way: The controller handles user interaction and presents views based on the model (actions + business).

** Typos corrected.

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