MVC,我做错了吗?

发布于 2024-12-25 16:20:30 字数 384 浏览 1 评论 0 原文

我正在尝试理解 MVC 的东西,到目前为止我知道它用于将业务逻辑与视图逻辑(例如 HTML 和 CSS)分开,但是当我需要组织文件时我失败了。

假设我有 3 个文件:

  • form.php 显示给用户,它需要用户输入并 提交数据
  • process.php,它从 form.php 获取并处理数据,然后 连接到数据库并检索请求的信息
  • display.php 以有组织的方式显示 process.php 中处理后的数据(结果)

所以看看我的示例:

  • form.php 将是控制器
  • process.php 将是模型,
  • display.php 将是视图

正确?

I'm trying to understand MVC thing and so far I know that it's used to separate business logic from the view logic (something as HTML and CSS), but I fail at point when I need to organize my files.

Lets say that I have 3 files:

  • form.php which is displayed to the user, it takes user input and
    submits data
  • process.php which takes and handle data from form.php, then
    connects to database and retrieve requested informations
  • display.php which display processed data (result) from process.php in organized way

So looking at my example:

  • form.php would be controller
  • process.php would be model and
  • display.php would be view

Right?

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

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

发布评论

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

评论(6

各自安好 2025-01-01 16:20:30

错误的,实际上你在process.php中混合了模型和控制器。

form.php 和 display.php 仅与用户交互,它们充当视图。

process.php 既充当控制器又充当模型

您应该将控制器和模型分开。您可以创建一个单独的 model.php 并在那里执行数据库操作。因此,如果将来您需要更改数据库内容。你不需要接触process.php。 Controller 和 Model 也将相互分离

Wrong, Actually you are mixing Model and Controller in process.php.

form.php and display.php are only interacting with user, they are acting as views.

process.php is acting as both Controller and Model

You should separate the Controller and Model. You can create a separte model.php and do the database stuff there. So if in future you need to change your database stuff. you dont need to touch process.php. Controller and Model will also be separated from each other

飘过的浮云 2025-01-01 16:20:30

我想说更像是

  • form.php - View
  • process.php - Controller
  • display.php - View

没有实际的模型。如果您有一个数据结构来表示 someDataClass.php 中的数据,那么这将是一个模型。

你想要的是分离 UI(视图)、数据处理(控制器)和数据定义(模型)。

I'd say more like

  • form.php - View
  • process.php - Controller
  • display.php - View

There is no actual model. If you have a data structure to represent the data in someDataClass.php, that would be a model.

What you want is to separate the UI (view), the data processing(controller) and the data definition(model).

没有你我更好 2025-01-01 16:20:30

不完全是。
process.php 是模型(它做艰苦的工作 - 处理数据库)
form.php和display.php是视图(显示给用户)

但是没有控制器。
控制器就像模型和视图之间的粘合剂。控制器从视图获取数据并说:“我将通过这个模型处理它”。处理后,它从模型中获取结果并说:“我将通过此视图向用户显示数据”

Not exactly.
process.php is model (It do hard work - working with database)
form.php and display.php is view (It is displayed to user)

But there is no controller.
Controller is something like a glue between model and view. Controller get data from View and say: "I will process it by this Model". And after processing it take result from Model and say:"I will display data to user by this View"

浅笑依然 2025-01-01 16:20:30

正如 rao_555 所说,这并不完全正确。例如,控制器不会有向用户显示的表单。如果您有一个表单,然后有一个数据显示,那么它们都将是单独的视图。

这是对设计模式的非常简洁的描述:
http://book.cakephp.org/2.0 /en/cakephp-overview/understanding-model-view-controller.html

This isn't really correct, as rao_555 says. The controller would not have a form that is shown to the user, for instance. If you have a form and then a data display, those would both be separate views.

This is a pretty concise description of the design pattern:
http://book.cakephp.org/2.0/en/cakephp-overview/understanding-model-view-controller.html

墨洒年华 2025-01-01 16:20:30

查看 维基百科 我也从这里开始了解 MVC。如果你想开发自己的 MVC 框架,你可以,但我强烈建议你首先学习 MVC 框架,它已经提供了很多选项(即 CodeIgniterYii 等)。祝你好运...

Check out Wikipedia me also started form here to Understand MVC. If you want to develop your own MVC framework you can but I strongly suggest you to first learn the MVC frameworks which already available with Lots of Options(i.e CodeIgniter, Yii etc). Best of luck...

肤浅与狂妄 2025-01-01 16:20:30

MVC 的解释相当开放。

但是,在您的情况下,很明显“显示”是您的 V(视图),“过程”是您的 M(模型)。

问题是……“表单”真的是“C”——控制器吗?

有几种方法可以看待这个问题。实际上,正确的方法是将您的表单投入使用(Symfony、Laravel 和 ZF2 使用的模式)。
然后,您将表单对象传递到视图以在 display.php 中呈现表单,一旦表单被 POST'ed,您将使用类似以下内容的内容:

// this will likely happen in your controller action 
$form->setData($postData);
$form->validate();

另一方面,在 CakePHP 中,表单进一步分解为验证和用户输入的“部分”。因此,验证成为“M”的一部分,而用户输入则保留在“V”中。

MVC is rather open to interpretation.

However, in your case it is somewhat clear that "display" is your V (View) and "process" is your M (Model).

The question is... is the "form" really a "C" -- controller?

There are a couple of ways to look at this. Actually, the proper way would be to make your form into service (a pattern used by Symfony, Laravel and ZF2).
Then you'd pass the form object down to your view to render the form in display.php and once the form is POST'ed you'd use something like:

// this will likely happen in your controller action 
$form->setData($postData);
$form->validate();

In CakePHP, on the other hand, form is further broken up into validation and user-input "parts". So validation becomes part of your "M" and user-input stays with the "V".

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