我可以将 MVC 设计模式应用到过程 PHP 中吗

发布于 2024-10-15 11:42:40 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(5

樱花坊 2024-10-22 11:42:40

MVC 是 OO 模式,您希望通过过程上下文来处理它。

这是完全错误的。 MVC 与面向对象编码无关。

MVC 是一种软件架构模式,旨在将信息的表示与用户的交互分开。

如何实现这一目标取决于您。您可以使用任何您想要的编码形式来实现这一目标,无论是面向对象的、过程的、函数式的还是其他形式。

关于手头的问题:当您编写过程性 PHP 代码时,实现 MVC 模式的最简单方法是使用许多小函数,其中每个特定函数都有自己独特的任务。不要让一个函数承担很多任务。这样就更容易分离事物。也不要在同一个文件中保留很多函数。而是将相关函数集中到较小的组中,每个组在其自己的文件中(实际上是在 OO 中使用类完成的)。

有人用一个简单的例子做到了这一点,没有 OO 的 MVC:http://www.fluffycat。 com/PHP-Design-Patterns/Non-OO-MVC/

MVC is OO pattern and you want to approach it with a procedural context.

This is plain wrong. MVC has nothing to do with object oriented coding.

MVC is a software architecture pattern which aims at separating the representation of information from the user's interaction with it.

How you achieve that is up to you. You can achieve that using whatever coding form you want, object oriented, procedural, functional, and what not.

With regard to the question at hand: The easiest way to achieve the MVC pattern when you're coding procedural PHP is to use a lot of small functions where each particular function has its own unique task. Don't let a function have many tasks. That way it is more easy to separate things. Also don't keep a lot of functions in the same file. Rather gather related functions together in smaller groups each group in its own file (what actually is done in OO with classes).

Someone does it here with a simple example, MVC without OO: http://www.fluffycat.com/PHP-Design-Patterns/Non-OO-MVC/

陌伤浅笑 2024-10-22 11:42:40

是的,这就是 MVC 的总结……但它不必是面向对象的……您只需要遵循一些黄金规则:

  1. 控制器接收并处理输入,生成任何数据并将其放入模型中。
  2. 视图从模型中获取数据并呈现它。
  3. 控制器不应该格式化视图的数据 - 它不应该知道视图想要什么/为什么/什么(例如,不要在文本字符串中插入 HTML,因为输出可能是 JSON)
  4. 视图不应该查找任何数据对于其本身 - 如果它不在模型中,则控制器在其工作中失败(抛出/报告错误)。

除此之外,你可以做任何你想做的事。您基本上需要一组过程来充当控制器 - 解析 $_REQUEST 变量(更可能为 GET/POST/COOKIE)执行任何数据查找构建 + 填充模型,然后另一组作为视图的过程 - 获取模型中的内容并将其呈现给用户。该模型可以像关联数组一样简单。

Yep, that about sums up MVC... but it doesn't have to be object oriented... you just need to follow a few golden rules:

  1. The controller receives and processes input, generates any data and puts it in the model.
  2. The view takes data from the model and renders it.
  3. The controller should not format data for view - it should have no knowledge of how/why/what the view wants (eg doesn't insert HTML in a text string since the output could be JSON)
  4. The view should not look-up any data for itself - if it's not in the model then the controller failed in it's job (throw/report an error).

Beyond that you can do things however you want. You'd basically need a set of procedures to act as controllers - parsing $_REQUEST vars (more likely as GET/POST/COOKIE) performing any data lookup building + filling the model, and then another set of procedures that at as views - taking what's in the model and rendering it for the user. The model can be as simple as an associative array.

二货你真萌 2024-10-22 11:42:40

绝对地。使用以下任意组合:

  1. 使用包含文件。
  2. 使用函数。
  3. 分离您的源文件。
  4. 在源文件中划分代码部分。
  5. 使用代码块(花括号)。
  6. 单独的 PHP 和/或 HTML 和/或 JS 文件。

任何能够更多地按照模型、视图和控制器来组织代码的东西。在某些人看来,它可能不是“纯粹的”或“正确的”MVC……但如果它更有意义地组织您的过程代码或教您更多有关 MVC 的知识,那么这是一件好事。

Absolutely. Use any combination of the following:

  1. Use include files.
  2. Use functions.
  3. Separate your source files.
  4. Divide sections of your code within source files.
  5. Use code blocks (curly braces).
  6. Separate PHP and/or HTML and/or JS files.

Anything that organizes your code more along the lines of Models, Views, and Controllers. It may not be "pure" or "correct" MVC in some people's minds...but if it organizes your procedural code more meaningfully or teaches you more about MVC, then it's a good thing.

﹏半生如梦愿梦如真 2024-10-22 11:42:40

这有点奇怪。
MVC 是 OO 模式,您希望通过过程上下文来处理它。

我首先想到的是拥有某种模板引擎,以便将 PHP 与 HTML 代码分开。这将是迈向过程 MVC 的一大步:)

接下来是用前缀命名同一组的函数(即包含相关对象逻辑的函数)(就像将方法分组在类名下一样)。

例如 - 查看用于 mysql 的 PHP 过程函数:

mysql_connect()
mysql_real_escape_string();
mysql_select_db();
mysql_query();

等。
并将这些函数分组到单独的文件中。这也将有助于解耦一些逻辑。

如果我还有其他想法,我会编辑我的帖子:)

This is kinda weird.
MVC is OO pattern and you want to approach it with a procedural context.

The first thing that comes to my mind is to have some kind of templating engine in order to split the PHP from the HTML code. This will be one big step towards procedural MVC :)

The next thing is to name functions of the same group (that means, functions that contains logic for related objects) with prefixes (like you group methods under a class name).

For example - look PHP procedural functions for working with mysql :

mysql_connect()
mysql_real_escape_string();
mysql_select_db();
mysql_query();

etc.
And group those functions in separate files. This will help to decouple a bit of the logic too.

If anything else come to my mind, i'll edit my post :)

柳若烟 2024-10-22 11:42:40

如果您正在实现新的设计模式,您很可能正在重构或从头开始编写一些东西。如果您打算使用 MVC,我强烈建议您转向 OOP。它可以按程序实现,但会非常hackish并且不是一个好的解决方案。

您可以轻松获取多种免费的 MVC php 解决方案。以下是一些:

  • Zend Framework
  • Kohana
  • Code Ignighter

If you are implementing a new design pattern you most likely are refactoring or writing something from scratch. I highly recommend moving to OOP if you plan to use MVC. It could be implemented procedurally but will be very hackish and not a good solution.

There are several free MVC php solutions that you can pick up easily. Here are a few:

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