php 一个简单网站的PHP文件结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3228268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:04:49  来源:igfitidea点击:

PHP file structure for a simple website?

phpstructure

提问by Victor Avasiloaei

I have been working with PHP for a couple of years now but I don't consider myself any more than a moderate programmer.

我已经使用 PHP 工作了几年,但我认为自己只不过是一个中等程序员。

While creating some websites (varying from presentation websites to some simple CMS'es), I have taken 2 approaches which I will discuss below. My question is simple: which one is better in terms of customization (it needs to be as customizable as possible) and speed (to load as fast as possible).

在创建一些网站时(从演示网站到一些简单的 CMS),我采用了两种方法,我将在下面讨论。我的问题很简单:在定制(它需要尽可能可定制)和速度(尽可能快地加载)方面哪个更好。

First choice

第一选择

files: header.php , content1.php, content2.php , footer.php

文件: header.php , content1.php, content2.php , footer.php

idea: all the content files include the header at the beginning of the file and the footer at the end.

想法:所有内容文件都包含文件开头的标题和结尾的页脚。



Second choice

第二选择

files: index.php , content1.php, content2.php

文件: index.php , content1.php , content2.php

idea: based on a GET variable or something similar, index.php includes the relevant php file

想法:基于 GET 变量或类似的东西,index.php 包含相关的 php 文件



Thanks in advance for the answer!

预先感谢您的回答!

回答by Martin Bean

I have a nifty framework that works equally well for small sites as it does large. The structure is pretty much the same in all instances and my directory structure looks as follows:

我有一个漂亮的框架,它既适用于大型网站,也适用于小型网站。所有实例的结构几乎相同,我的目录结构如下所示:

.htaccess
inc/
tpl/
    css/
    images/
    js/
index.php

The job of index.phpis to determine what file to load, where any programming logic is held in files in the incdirectory, and templates are help in the tpldirectory. For example, index.phpcould be as simple as this:

的工作index.php是确定要加载的文件,inc目录中的文件中保存任何编程逻辑的位置,以及目录中的模板帮助tpl。例如,index.php可以像这样简单:

<?php
switch ($_GET['filename']) {
    case 'news':
        require('inc/news.php');      // your news functions
        include('tpl/news.tpl.php');  // your news template
    break;
    case 'events':
        require('inc/events.php');
        include('tpl/events.tpl.php');
    break;
    case 'contact':
        require('inc/contact.php');
        include('tpl/contact.tpl.php');
    break;
    default:
        if ($_GET['filename'] == '') {
            include('tpl/home.tpl.php');
        }
        else {
            header('HTTP/1.0 404 Not Found');
            include('tpl/page_not_found.tpl.php');
        }
    break;
}

Coupled with the following .htaccessrules:

再加上以下.htaccess规则:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php?filename=

Hopefully that makes sense. If not, let me know what doesn't and I'll be happy to explain further.

希望这是有道理的。如果没有,请告诉我什么不是,我很乐意进一步解释。

回答by DrColossos

I would go with the 2nd one. This is the approach taken by many frameworks and a very good one. You have a single place that get's always called, so you could do some processing in there like cleaning up the URL, checking for a valid Session,...

我会选择第二个。这是许多框架采用的方法,也是一种非常好的方法。你有一个地方总是被调用,所以你可以在那里做一些处理,比如清理 URL,检查有效的会话,......

Furthermore, you can store some basic options inside the index.php (since it's a small site I see ne problem in this way of doing it but with bigger projects, sperate config files are the better way IMO) that can be accessed by all pages that are getting called (included) from within the index.phpfile.

此外,您可以在 index.php 中存储一些基本选项(因为它是一个小站点,我认为这样做有问题,但对于更大的项目,单独的配置文件是 IMO 的更好方式),所有页面都可以访问这些选项从index.php文件中被调用(包括)。

Speed should not be an issue with a small site so whatever solution you may choose in the end will have good results.

对于小型站点,速度应该不是问题,因此您最终选择的任何解决方案都会产生良好的效果。

回答by Tapdingo

If the website really is simple and doesn't need any additional utility, i would go with the second choice. Mostly the HTML is not so complicated, that it has to be splitted into several files, the actual presentation magic should rest in the CSS file anyway... With the second choice you have your - simple - website in one place and don't have to look around in several files if you want to edit something.

如果网站真的很简单并且不需要任何额外的实用程序,我会选择第二个选择。大多数情况下,HTML 并没有那么复杂,它必须被拆分成几个文件,无论如何,实际的演示魔法都应该在 CSS 文件中......使用第二个选择,您可以将您的 - 简单 - 网站放在一个地方,而不是如果你想编辑一些东西,必须在几个文件中环顾四周。

回答by jim tollan

Victorelu - not really an answer to your question. But you might be advised to begin looking at some of the lighter weight MVC php frameworks (i'm not talking about CI, kohana, cake etc, more the very lightweight ones such as caffeine). I did a little proof of concept using caffeine (a lightweight MVC framework based on a few core files) that neatly addresses the requirement that you state:

Victorelu - 不是你问题的真正答案。但是可能会建议您开始查看一些轻量级的 MVC php 框架(我不是在谈论 CI、kohana、蛋糕等,更多的是非常轻量级的框架,例如咖啡因)。我使用咖啡因(一个基于几个核心文件的轻量级 MVC 框架)做了一些概念证明,它巧妙地满足了您陈述的要求:

'... customization'.

'...定制'。

jim

吉姆

[edit]- link to caffeine: http://code.google.com/p/caffeine-php/there's agreat little pdf that walks the entire process: http://code.google.com/p/caffeine-php/downloads/list

[编辑]- 咖啡因链接:http: //code.google.com/p/caffeine-php/有一个很棒的小 pdf 文件可以完成整个过程:http: //code.google.com/p/caffeine-php/下载/列表

回答by Jaison Justus

i didnt use any framework for my projects. but everyone advice me to use a framework. i maintain a file/directory structure of my own

我没有为我的项目使用任何框架。但每个人都建议我使用框架。我维护自己的文件/目录结构

INSIDE ROOT DIR

内部根目录

  • /backups
  • /config
  • /library
  • /media
  • /models
  • /modules
  • /templates
  • /sql files
  • index.php
  • /备份
  • /配置
  • /图书馆
  • /媒体
  • /楷模
  • /模块
  • /模板
  • /sql 文件
  • 索引.php

回答by Girdhari Choyal

Check mofe information about website page structure.

检查有关网站页面结构的 mofe 信息。

The most important and very basic part of php website is page structure of php website because it help when you want to redesign website or you want to edit some code then sure you need to know page structure of website.

php 网站最重要和最基本的部分是 php 网站的页面结构,因为当您想重新设计网站或编辑一些代码时,它会有所帮助,然后确定您需要了解网站的页面结构。

Check full detail here

在此处查看完整详细信息