为什么整个网站使用单个 index.php 页面?
我正在接管一个现有的 PHP 项目。我注意到前一位开发人员对整个网站使用一个 index.php 页面,目前有 10 多个页面。这是我见过的第二个这样完成的项目。我没有看到这种方法的优势。事实上,它似乎使一切变得过于复杂,因为现在您不能只向网站添加新页面并链接到它。您还必须确保使用 if 子句更新主索引页以检查该页面类型,然后加载该页面。看来,如果他们只是尝试重用模板,那么仅使用页眉和页脚的包含内容,然后使用引用的这些文件创建每个新页面会更容易。
有人可以解释为什么要使用这种方法吗?这是我不熟悉的某种形式的 MVC 模式吗? PHP 是第二语言,所以我不太熟悉最佳实践。
我尝试在 Google 中搜索“带有 php 的单一索引页面”之类的内容,但我找不到任何好的文章来解释为什么使用这种方法。我真的很想把这些旧东西踢到路边,而不是继续沿着这条路走下去,但我想在提出建议之前有一些合理的推理。
I am taking over an existing PHP project. I noticed that the previous developer uses a one index.php page for the entire site, currently 10+ pages. This is the second project that I have seen done like this. I don't see the advantage with this approach. In fact it seems like it over complicates everything because now you can't just add a new page to the site and link to it. You also have to make sure you update the main index page with a if clause to check for that page type and then load the page. It seems if they are just trying to reuse a template it would be easier to just use includes for the header and footer and then create each new page with those files referenced.
Can someone explain why this approach would be used? Is this some form of an MVC pattern that I am not familiar with? PHP is a second language so I am not as familiar with best practices.
I have tried doing some searches in Google for "single index page with php" and things like that but I can not find any good articles explaining why this approach is being used. I really want to kick this old stuff to the curb and not continue down that path but I want to have some sound reasoning before making the suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
前端控制器 (index.php) 确保整个站点共有的所有内容(例如身份验证)始终得到正确处理,无论您请求哪个页面。如果您有 50 个不同的 PHP 文件分散在各处,那么管理起来就会很困难。如果您决定更改公共库文件的加载顺序怎么办?如果您只有一个文件,则可以在一处更改它。如果您有 50 个不同的入口点,则需要全部更改。
有人可能会说,一直加载所有常见的内容是浪费资源,您应该只加载该特定页面所需的文件。真的。但如今的 PHP 框架大量使用 OOP 和自动加载,因此这种“浪费”已不复存在。
前端控制器还使您可以轻松地在站点中拥有漂亮的 URL,因为您可以完全自由地使用您喜欢的任何 URL 并将其发送到您需要的任何控制器/方法。否则,您将陷入每个以
.php
结尾的 URL,后跟丑陋的查询字符串列表,而避免这种情况的唯一方法是在 .htaccess 文件中使用更丑陋的重写规则。即使是具有数十个不同入口点(尤其是在管理部分)的 WordPress,也会强制最常见的请求通过 index.php,以便您可以拥有灵活的永久链接格式。几乎所有其他语言的 Web 框架都使用单点入口——或者更准确地说,调用单个脚本来引导一个进程,然后该进程与 Web 服务器进行通信。姜戈就是这样工作的。 CherryPy 就是这样工作的。在 Python 中这样做是很自然的。 PHP 是唯一一种广泛使用的允许以任何其他方式编写 Web 应用程序(除非用作旧式 CGI 脚本)的语言。在 PHP 中,您可以为任何文件提供
.php
扩展名,它将由 Web 服务器执行。这是非常强大的,并且它使得 PHP 很容易学习。但是,一旦复杂程度超过一定程度,单点进入方法就开始显得更具吸引力。A front controller (index.php) ensures that everything that is common to the whole site (e.g. authentication) is always correctly handled, regardless of which page you request. If you have 50 different PHP files scattered all over the place, it's difficult to manage that. And what if you decide to change the order in which the common library files get loaded? If you have just one file, you can change it in one place. If you have 50 different entry points, you need to change all of them.
Someone might say that loading all the common stuff all the time is a waste of resources and you should only load the files that are needed for this particular page. True. But today's PHP frameworks make heavy use of OOP and autoloading, so this "waste" doesn't exist anymore.
A front controller also makes it very easy for you to have pretty URLs in your site, because you are absolutely free to use whatever URL you feel like and send it to whatever controller/method you need. Otherwise you're stuck with every URL ending in
.php
followed by an ugly list of query strings, and the only way to avoid this is to use even uglier rewrite rules in your .htaccess file. Even WordPress, which has dozens of different entry points (especially in the admin section), forces most common requests to go through index.php so that you can have a flexible permalink format.Almost all web frameworks in other languages use single points of entry -- or more accurately, a single script is called to bootstrap a process which then communicates with the web server. Django works like that. CherryPy works like that. It's very natural to do it this way in Python. The only widely used language that allows web applications to be written any other way (except when used as an old-style CGI script) is PHP. In PHP, you can give any file a
.php
extension and it'll be executed by the web server. This is very powerful, and it makes PHP easy to learn. But once you go past a certain level of complexity, the single-point-of-entry approach begins to look a lot more attractive.在公共目录中拥有单个 index.php 文件也可以防止 php 解释器出现故障。许多框架使用index.php 文件将引导文件包含在文档根目录之外。如果发生这种情况,用户将能够看到这个单个文件的源代码,而不是整个代码库。
Having a single index.php file in the public directory can also protect against in the case of the php interpreter going down. A lot of frameworks use the index.php file to include the bootstrap file outside of the doc root. If this happens, the user will be able to see your sourcecode of this single file instead of the entire codebase.
好吧,如果唯一改变的是 URL,那么除了美观目的之外,似乎没有任何原因......
Well, if the only thing that changes is the URL, It doesn't seem like it's done for any reason besides aesthetic purposes...
对我来说 - 单一入口点可以帮助您更好地控制您的应用程序:它有助于轻松处理错误、路由请求、调试应用程序。
As for me - single entry point can help you to have better control of your application: it helps to handle errors easily, route requests, debug application.
单个“index.php”是确保对应用程序的所有请求都通过同一入口的简单方法。这样,当您添加第二个页面时,您不必确保引导、身份验证、授权、日志记录等都已配置——您可以通过框架免费获得它。
在现代 Web 框架中,这可能使用前端控制器,但由于许多 PHP 代码/开发人员患有 NIH 综合症,因此无法判断。
A single "index.php" is an easy way to make sure all requests to your application flow through the same gate. This way when you add a second page you don't have to make sure bootstrapping, authentication, authorization, logging, etc are all configured--you get it for free by merit of the framework.
In modern web frameworks this could be using a front controller but it is impossible to tell since a lot of PHP code/developers suffer from NIH syndrome.
通常,当页面内容由数据库内容确定时,使用此类方法。因此,所有工作都将在一个文件中完成。这在 CMS 系统中很常见。
Typically such approaches are used when the contents of the pages are determined by database contents. Thus all the work would get done in a single file. This is seen often in CMS systems.