PHP 中的表示和业务逻辑分离

发布于 2024-09-05 22:13:12 字数 1323 浏览 2 评论 0原文

我正在编写我的第一个真正的 PHP 网站,并且想知道如何使我的代码对自己更具可读性。我使用的参考书是《PHP and MySQL Web Development 第四版》。

前面提到的书给出了三种分离逻辑和内容的方法:

  • 包含文件、
  • 函数或类 API
  • 模板系统,

我还没有选择其中任何一个,因为理解这些概念需要一些时间。然而,我的代码已经成为前两者的混合体,因为我只是在这里复制粘贴并随时修改。

在演示方面,我的所有页面都有这些通用元素:标题、顶部导航、侧边栏导航、内容、右侧边栏和页脚。

书中基于函数的示例表明我可以使用这些显示函数来处理所有演示示例。所以,我的页面代码将是这样的:

display_header();
display_navigation();
display_content();
display_footer();

但是,我不喜欢这样,因为书中的示例将这些打印语句与 HTML 和 PHP 混合在一起,如下所示:

echo "<tr bgcolor=\"".$color."\"><td><a href=\"".$url."\">" ...

我宁愿将 HTML 与一些 PHP 放在中间,而不是相反。

我正在考虑制作我的页面,以便在页面的开头,我将从数据库中获取所有数据并将其放入数组中。我还将获取变量的数据。如果这些过程中有任何错误,我会将它们放入错误字符串中。

然后,在 HTML 代码中,我将使用 foreach 循环访问这些数组并显示内容。在某些情况下,会显示一些变量。如果设置了错误变量,我会将其显示在正确的位置。

(作为旁注:我不明白的是,在大多数示例代码中,如果某些数据库查询或其他什么给出错误,总是:

else echo 'Error';

这让我感到困惑,因为当示例代码给出错误时,有时会回显甚至在 HTML 开始之前就出现了...)

对于使用过 ASP.NET 的人来说,我已经有点习惯了代码隐藏文件和 lblError,并且我正在尝试在这里做类似的事情。

我还没弄清楚的是,我怎样才能做到“先做逻辑,然后演示”的事情,这样我就不必在所有页面中复制例如导航逻辑和导航演示。

我应该做一些包含文件还是可以在这里使用函数但有点不同?是否有任何好的文章可以更彻底地解释这些分离表示和逻辑的“风格”。我的书里只有一段是关于这个东西的。

我的想法是,我在这里谈论一些进行 PHP 编程的概念或方法,但我只是还不知道它们的术语。

我知道这不是一个直接的问题,我只是需要一些帮助来组织我的想法。

I am programming my first real PHP website and am wondering how to make my code more readable to myself. The reference book I am using is PHP and MySQL Web Development 4th ed.

The aforementioned book gives three approaches to separating logic and content:

  • include files
  • function or class API
  • template system

I haven't chosen any of these yet, as wrapping my brains around these concepts is taking some time. However, my code has become some hybrid of the first two as I am just copy-pasting away here and modifying as I go.

On presentation side, all of my pages have these common elements: header, top navigation, sidebar navigation, content, right sidebar and footer.

The function-based examples in the book suggest that I could have these display functions that handle all the presentation example. So, my page code will be like this:

display_header();
display_navigation();
display_content();
display_footer();

However, I don't like this because the examples in the book have these print statements with HTML and PHP mixed up like this:

echo "<tr bgcolor=\"".$color."\"><td><a href=\"".$url."\">" ...

I would rather like to have HTML with some PHP in the middle, not the other way round.

I am thinking of making my pages so that at the beginning of my page, I will fetch all the data from database and put it in arrays. I will also get the data for variables. If there are any errors in any of these processes, I will put them into error strings.

Then, at the HTML code, I will loop through these arrays using foreach and display the content. In some cases, there will be some variables that will be shown. If there is an error variable that is set, I will display that at the proper position.

(As a side note: The thing I do not understand is that in most example code, if some database query or whatnot gives an error, there is always:

else echo 'Error';

This baffles me, because when the example code gives an error, it is sometimes echoed out even before the HTML has started...)

For people who have used ASP.NET, I have gotten somewhat used to the code-behind files and lblError and I am trying to do something similar here.

The thing I haven't figured out is how could I do this "do logic first, then presentation" thing so that I would not have to replicate for example the navigation logic and navigation presentation in all of the pages.

Should I do some include files or could I use functions here but a little bit differently? Are there any good articles where these "styles" of separating presentation and logic are explained a little bit more thoroughly. The book I have only has one paragraph about this stuff.

What I am thinking is that I am talking about some concepts or ways of doing PHP programming here, but I just don't know the terms for them yet.

I know this isn't a straight forward question, I just need some help in organizing my thoughts.

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

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

发布评论

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

评论(3

献世佛 2024-09-12 22:13:12

从不使用 PHP 回显 HTML。而是将其内联编写(没有邪恶的短标签)为

其他有助于帮助的选项分离出逻辑/视图将使用 PHP 框架,例如 CodeIgniter

我会放弃这本书,而是更多地专注于学习核心 PHP 技能,如函数、类等。然后开始玩 几个流行的框架

作为旁注:我不知道的事情
理解的是,在大多数例子中
代码,如果某些数据库查询或
什么会给出错误,有
总是:

那是因为他们错误地显示了错误。您应该将错误存储在会话中,然后将它们显示在页面上(也清除它们),或者使用 error_log 函数将它们放入错误日志中。 error_log("MyClass 发生了一些事情");

我还没弄清楚的是如何
我可以这样做“先做逻辑,然后
演示”的事情,这样我就会
不必复制例如
导航逻辑和导航
所有页面中的演示。

MVC 方法思考事物。您首先调用控制器(逻辑)。它弄清楚需要什么。如果它需要来自数据库的数据,它会调用模型并请求它。然后它对其进行格式化,添加其他数据,运行其他查询,然后将其传递到视图。

Never echo out HTML with PHP. Instead write it inline (without evil short tags) as

<tr class="<?php echo $myclass; ?>">

Other options for helping to separate out the logic / view would be to use a PHP Framework like CodeIgniter.

I would ditch the book and instead focus more on learning core PHP skills like functions, classes, etc. Then start playing the the several popular frameworks out there.

As a side note: The thing I do not
understand is that in most example
code, if some database query or
whatnot gives an error, there is
always:

That's because they are displaying the errors incorrectly. You should either store the errors in a sesssion and then display them on the page (clearing them as well) or throw them into the error log with the error_log function. error_log("Something happened in MyClass");

The thing I haven't figured out is how
could I do this "do logic first, then
presentation" thing so that I would
not have to replicate for example the
navigation logic and navigation
presentation in all of the pages.

Think of things in a MVC approach. You call the controller (logic) first. It figures out what is needed. If it needs data from the database it invokes a Model and requests it. Then it formats it, adds other data, runs additional queries, and then passes it to the view.

樱桃奶球 2024-09-12 22:13:12

听起来你正在寻找一个模板引擎 - 向谷歌询问很多结果。就我个人而言,我非常喜欢smarty

(然后扔掉那本书,听起来像是……旧的)

sound like a template engine is what you are looking for - ask google for a lot of results. personally, i like smarty very much.

(and throw away that book, sound like it's... old)

巨坚强 2024-09-12 22:13:12

MVC(模型视图控制器)听起来可能适合您的需求。您可以此处阅读相关内容

MVC (Model View Controller) sounds like it might suit your needs. You can read about that here.

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