你如何在 php 中组织你的页面?

发布于 2024-11-05 15:59:28 字数 492 浏览 1 评论 0原文

通常我有主页(index.php),其中包括页眉/页脚/菜单和页面内容。通过检查 GET/POST 方法中的某些变量来更改内容:对于每个条件,我加载请求的页面。

像:

website/
website/index.php?explore=forum
website/index.php?explore=users
website/index.php?explore=articles

等等...

现在,我的网站正在增长,我认为最好的方法是直接为每个部分调用“索引”页面;因此,例如上面的例子,这将被翻译为:

website/
website/forum/
website/users/
website/articles/

看起来更好,并且更易于管理!问题是,如果我这样做,我需要为每个区域(包括页眉/页脚/菜单)实现轮廓。

所以不知道有没有更好的策略,是否方便。欢迎任何建议/意见!谢谢

Usually I have the main page (index.php) that include header/footer/menu and the content of the page. The content is changed checking some variable from the GET/POST method: for each condition, I load the page requested.

Somethings like :

website/
website/index.php?explore=forum
website/index.php?explore=users
website/index.php?explore=articles

and so on...

Now, my website is growing, and I think the best way is to call directly a "index" page for each section; so for example above, this will be traslate as :

website/
website/forum/
website/users/
website/articles/

that looks better, and is more confortable to manage! The problem is that, if I do this, I need to implement the contour for each zone (that include header/footer/menu).

So I don't know if there are a better strategy and if its convenient. Any suggestions/opinions is welcome! Thanks

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

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

发布评论

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

评论(4

梦里的微风 2024-11-12 15:59:28

您可以通过进行 apache 重写,使用顶部策略获得底部结果。例如,/forum 可以重写为访问 www.yoursite.com/index.php?article=forum 等。

这是完整的指南: http://httpd.apache.org/docs/2.0/杂项/rewriteguide.html

You could get the bottom result with the top strategy by doing apache rewrites. For example, /forum could be re-written to go to www.yoursite.com/index.php?article=forum and so forth.

Here's the complete guide: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

美人骨 2024-11-12 15:59:28

查看 Symfony (PHP)、Rails (Ruby) 或 Django (Python) 等 MVC 框架。

在 MVC 设计模式中,通常有一个前端控制器来处理所有加载。您拥有与数据源交互的模型,并且拥有定义用户界面的视图。控制器通过模型加载数据,然后将这些数据传递给视图。

在您的示例中,每个部分都有不同的控制器,不同的交互类型(最常见的是创建、列表、删除、编辑)具有不同的操作。

Check out an MVC framework like Symfony (PHP), Rails (Ruby) or Django (Python).

In the MVC design pattern, you typically have one front controller which handles all the loading. You have models which interact with your data sources, and you have views which define the user interface. Controllers load data via the models, and then pass these to views.

In your example, each section would have a different controller, and different interaction types (the most common being create, list, delete, edit) have different actions.

吖咩 2024-11-12 15:59:28

您可以使用 .htaccess 重写规则将这些漂亮的 URL 重定向到单个 index.php 文件。

唯一的问题是您必须想出一种方法来区分要重定向的 URL(website/forum/website/users/ 等)普通的(website/css/website/images/ 等)。

有一些选项:

  1. 您可以使用 website/page/[pagename]/,而不是使用 website/[pagename]/。然后你的正则表达式可以将每个匹配 ^/?page/[\w+]/?$ 的 URL 重定向到 index.php?explore=[pagename]
  2. 只需显式列出每个您要重定向的页面;使用像 ^/?[forum|users|articles]/?$
  3. 这样的正则表达式 别担心。如果您仅重定向不包含文件名的 URL,那么当您尝试引用 CSS 或图像时(即包含文件名的 URL) ,这不应该是一个问题。

You could just use .htaccess rewrite rules to redirect those nice-looking URLs to a single index.php file.

The only problem is that you'd have to come up with a way of differentiating the URLs to redirect (website/forum/, website/users/, etc.) from the normal ones (website/css/, website/images/, etc.).

There are some options:

  1. Instead of using website/[pagename]/, you could use website/page/[pagename]/. Then your regex could just redirect every URL that matches ^/?page/[\w+]/?$ to index.php?explore=[pagename]
  2. Just explicitly list every page that you want to redirect; use a regex like ^/?[forum|users|articles]/?$
  3. Don't worry about it. If you're only redirecting URLs that don't include a file name, then when you do try to reference CSS or images (i.e., URLs that do include a filename), it shouldn't be an issue.
2024-11-12 15:59:28

在我们的直接 PHP 项目中(没有像 Symfony 或其他框架这样的框架),我们肯定为每个部分都有不同的目录和单独的索引文件,就像你说的那样。

但我们通常只有一个页眉包含文件和一个页脚文件,适用于网站上的每个页面。这使页眉和页脚保持一致,并且更易于管理。

您始终可以在页眉和页脚中包含一行,

<?php include('../includes/head.php'); ?>

就像您想将这些文件保留在自己的目录中一样。我认为我们的包含路径上实际上有 /includes 所以我们的行只是说

<?php include('head.php'); ?>

In our projects that are straight PHP (no framework like Symfony or whatever), we definitely have different directories and separate index files for each section, like you say.

But we generally just have one header include file, and one footer file, that applies to every page on the site. That keeps the header and footer consistent, and more manageable.

You can always include the header and footer with a line like

<?php include('../includes/head.php'); ?>

if you want to keep those files in their own directory. I think we actually have /includes on our include path so our lines just say

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