简体   繁体   English

为什么要对整个网站使用单个index.php页面?

[英]Why use a single index.php page for entire site?

I am taking over an existing PHP project. 我要接管一个现有的PHP项目。 I noticed that the previous developer uses a one index.php page for the entire site, currently 10+ pages. 我注意到以前的开发人员在整个网站上使用一个index.php页面,目前使用10多个页面。 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. 您还必须确保使用if子句更新主索引页面以检查该页面类型,然后加载该页面。 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. 看来,如果他们只是想重用模板,则将includes用作页眉和页脚,然后使用所引用的文件创建每个新页面会更容易。

Can someone explain why this approach would be used? 有人可以解释为什么会使用这种方法吗? Is this some form of an MVC pattern that I am not familiar with? 这是我不熟悉的某种形式的MVC模式吗? PHP is a second language so I am not as familiar with best practices. PHP是第二语言,因此我对最佳实践不太熟悉。

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. 我曾尝试在Google中搜索“使用php的单个索引页面”之类的方法,但是我找不到任何好的文章来解释为什么使用这种方法。 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 (eg authentication) is always correctly handled, regardless of which page you request. 前端控制器(index.php)可确保始终正确处理整个站点共有的所有内容(例如,身份验证),无论您请求哪个页面。 If you have 50 different PHP files scattered all over the place, it's difficult to manage that. 如果您到处散布了50个不同的PHP文件,则很难进行管理。 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. 如果您有50个不同的入口点,则需要全部更改。

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. 但是,当今的PHP框架大量使用了OOP和自动加载功能,因此不再存在这种“浪费”。

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. 前端控制器还使您很容易在站点中拥有漂亮的URL,因为您绝对可以随意使用自己喜欢的任何URL,并将其发送到所需的任何控制器/方法。 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. 否则,您将陷入以.php结尾的每个URL后面紧跟丑陋的查询字符串列表的情况,而避免这种情况的唯一方法是在.htaccess文件中使用甚至更丑陋的重写规则。 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. 即使是WordPress,它具有数十种不同的入口点(尤其是在admin部分中),也会强制大多数常见的请求通过index.php进行访问,以便您可以使用灵活的永久链接格式。

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. 几乎所有其他语言的Web框架都使用单个入口点-或更准确地说,调用单个脚本来引导进程,然后该进程与Web服务器进行通信。 Django works like that. Django就是这样。 CherryPy works like that. CherryPy就是这样。 It's very natural to do it this way in Python. 用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. 使Web应用程序以任何其他方式编写(除非用作老式CGI脚本时)的唯一广泛使用的语言是PHP。 In PHP, you can give any file a .php extension and it'll be executed by the web server. 在PHP中,您可以为任何文件赋予.php扩展名,然后它将由网络服务器执行。 This is very powerful, and it makes PHP easy to learn. 这非常强大,并且使PHP易于学习。 But once you go past a certain level of complexity, the single-point-of-entry approach begins to look a lot more attractive. 但是一旦您超过了一定程度的复杂性,单进入点的方法就会开始变得更具吸引力。

Having a single index.php file in the public directory can also protect against in the case of the php interpreter going down. 在公共目录中只有一个index.php文件还可以防止php解释器崩溃。 A lot of frameworks use the index.php file to include the bootstrap file outside of the doc root. 许多框架使用index.php文件在doc根目录之外包含bootstrap文件。 If this happens, the user will be able to see your sourcecode of this single file instead of the entire codebase. 如果发生这种情况,用户将可以查看该单个文件的源代码,而不是整个代码库。

好吧,如果唯一改变的是URL,那么除了出于美观目的之外,它似乎并没有出于任何原因而完成...

对于我来说-单个入口点可以帮助您更好地控制应用程序:它可以轻松处理错误,路由请求,调试应用程序。

A single "index.php" is an easy way to make sure all requests to your application flow through the same gate. 单个“ index.php”是确保对应用程序的所有请求都流经同一门的简单方法。 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. 在现代Web框架中,这可能使用前端控制器,但由于许多PHP代码/开发人员都患有NIH综合症,因此无法确定。

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. 这在CMS系统中很常见。

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

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