为什么整个网站使用一个index.php页面? [英] Why use a single index.php page for entire site?

查看:97
本文介绍了为什么整个网站使用一个index.php页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接管现有的PHP项目。我注意到,以前的开发人员为整个网站使用一个index.php页面,目前有10多个页面。这是我看到的第二个这样做的项目。我没有看到这种方法的优势。事实上,似乎它使所有的事情变得复杂化,因为现在你不能只向网站添加一个新的页面并链接到它。您还必须确保使用if子句更新主索引页,以检查该页类型,然后加载该页。看来,如果他们只是想重新使用一个模板,那么使用头文件和页脚就可以更容易地使用这些文件,然后用引用的这些文件创建每个新页面。



有人可以解释为什么会使用这种方法吗?这是我不熟悉的某种形式的MVC模式吗? PHP是第二种语言,所以我不太熟悉最佳做法。



我曾经尝试过在Google搜索php的单一索引页面和类似的内容但我找不到任何好的文章解释为什么这种方法被使用。我真的想把这些旧东西踢到路边,而不是继续下去,但是在提出建议之前,我想要有一些合理的推理。

解决方案

前端控制器(index.php)可以确保整个站点(例如身份验证)常见的一切始终是正确的处理,无论您要求哪个页面。如果你有50个不同的PHP文件分散在这个地方,很难管理。如果您决定更改通用库文件加载的顺序怎么办?如果你只有一个文件,你可以在一个地方改变它。如果您有50个不同的入口点,则需要更改所有这些。



有人会说,一直加载所有常见的东西是浪费资源,你应该只加载这个特定页面所需的文件。真正。但是今天的PHP框架大量使用OOP和自动加载,所以这个浪费不再存在。



前端控制器也使您很容易您的网站中的漂亮网址,因为您绝对可以随意使用任何您感觉到的网址,并将其发送到您需要的任何控制器/方法。否则,您将停留在 .php 中的每个网址,后跟一个丑陋的查询字符串列表,避免这种情况的唯一方法是在您的.htaccess文件。即使WordPress也有几十个不同的入口点(特别是在管理部分),因此大多数常见请求都会通过index.php来实现,以便您可以使用灵活的永久链接格式。



几乎所有其他语言的Web框架都使用单个入口点,或者更准确地说,单个脚本被称为引导一个进程,然后与Web服务器进行通信。 Django像这样工作。 CherryPy这样工作。在Python中这样做很自然。唯一广泛使用的语言,允许Web应用程序以其他方式编写(除非用作旧式CGI脚本)是PHP。在PHP中,您可以给任何文件一个 .php 扩展名,它将由Web服务器执行。这是非常强大的,它使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.

解决方案

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页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆