简体   繁体   English

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

[英]How do you organize your page in php?

Usually I have the main page (index.php) that include header/footer/menu and the content of the page. 通常我有主页面(index.php),包括页眉/页脚/菜单和页面的内容。 The content is changed checking some variable from the GET/POST method: for each condition, I load the page requested. 更改内容从GET / POST方法检查一些变量:对于每个条件,我加载请求的页面。

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 谢谢

You could get the bottom result with the top strategy by doing apache rewrites. 通过执行apache重写,您可以获得最高策略的最终结果。 For example, /forum could be re-written to go to www.yoursite.com/index.php?article=forum and so forth. 例如,可以重写/ forum以访问www.yoursite.com/index.php?article=forum等。

Here's the complete guide: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html 这是完整的指南: http//httpd.apache.org/docs/2.0/misc/rewriteguide.html

Check out an MVC framework like Symfony (PHP), Rails (Ruby) or Django (Python). 查看一个MVC框架,如Symfony(PHP),Rails(Ruby)或Django(Python)。

In the MVC design pattern, you typically have one front controller which handles all the loading. 在MVC设计模式中,您通常有一个前端控制器来处理所有负载。 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. 在您的示例中,每个部分都有不同的控制器,不同的交互类型(最常见的是创建,列表,删除,编辑)具有不同的操作。

You could just use .htaccess rewrite rules to redirect those nice-looking URLs to a single index.php file. 您可以使用.htaccess重写规则将这些漂亮的URL重定向到单个index.php文件。

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.). 唯一的问题是你必须想出一种方法来区分URL重定向( website/forum/website/users/等)与正常的website/css/website/css/website/images/ ,等等。)。

There are some options: 有一些选择:

  1. Instead of using website/[pagename]/ , you could use website/page/[pagename]/ . 您可以使用website/page/[pagename]/而不是使用website/[pagename]/ website/page/[pagename]/ Then your regex could just redirect every URL that matches ^/?page/[\\w+]/?$ to index.php?explore=[pagename] 然后你的正则表达式可以将每个匹配^/?page/[\\w+]/?$ URL重定向到index.php?explore=[pagename]
  2. Just explicitly list every page that you want to redirect; 只需明确列出要重定向的每个页面; use a regex like ^/?[forum|users|articles]/?$ 使用像^/?[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 (ie, URLs that do include a filename), it shouldn't be an issue. 如果你只重定向的URL不包括文件名,那么当尝试引用CSS或图像(即, 包括文件名的URL),它不应该是一个问题。

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. 在我们的项目中,它们是直接的PHP(没有像Symfony这样的框架),我们肯定会为每个部分提供不同的目录和单独的索引文件,就像你说的那样。

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'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM