对于大型站点,基于 PHP 的最具可扩展性的目录结构是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1387547/
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
What is the most scalable PHP-based directory structure for a large site?
提问by johnnietheblack
I am creating a very large PHP MVC-based site that will have a large library of php classes, javascripts, and many css files (not to mention a large amount of files for the MVC).
我正在创建一个非常大的基于 PHP MVC 的站点,它将拥有一个包含 php 类、javascripts 和许多 css 文件的大型库(更不用说 MVC 的大量文件)。
For the first time ever, I am actually taking the time to plan out a clean and organized directory structure.
有史以来第一次,我实际上花时间规划一个干净且有组织的目录结构。
What directory structures do you typically use, and which will be easiest to manuever when there are thousands of files?
您通常使用什么目录结构,当有数千个文件时,哪种目录结构最容易操作?
采纳答案by Steven Mercatante
This is my setup. It's worked great for me for small - very large projects (including a social network).
These folders would all live within my main application folder:
这是我的设置。对于小型 - 非常大型的项目(包括社交网络),它对我来说非常有用。
这些文件夹都位于我的主应用程序文件夹中:
- config - contains custom PHP config files
- css - contains the project's CSS files
- helpers - contains 'helper' files (each file is a collection of functions)
- images - contains the project's images
- js - contains the project's Javascript files
- lib - contains PHP classes specific to the project
- modules - My MVC framework allows packaging site sections as modules
- blog - An example module
- controllers - contains the controllers for the module
- models - contains the models for the module
- views - contains the views for the module
- blog - An example module
- views - contains views that should be globally accessible (page header, footer, etc)
- config - 包含自定义 PHP 配置文件
- css - 包含项目的 CSS 文件
- helpers - 包含“helper”文件(每个文件都是函数的集合)
- 图像 - 包含项目的图像
- js - 包含项目的 Javascript 文件
- lib - 包含特定于项目的 PHP 类
- 模块 - 我的 MVC 框架允许将站点部分打包为模块
- 博客 - 一个示例模块
- 控制器 - 包含模块的控制器
- 模型 - 包含模块的模型
- 视图 - 包含模块的视图
- 博客 - 一个示例模块
- 视图 - 包含应该全局访问的视图(页眉、页脚等)
All the directories could obviously contain sub-folders that would further organize your files. For example, the 'css' folder could have sub-folders named 'web' and 'mobile'. The 'images' folder could contain a 'user_uploaded' folder which could then contain`'profile'. And of course you can add folders as you see fit, in one project I have a folder called 'uploaders' which just contains stand-alone upload scripts.
所有目录显然都可以包含可以进一步组织文件的子文件夹。例如,“css”文件夹可以有名为“web”和“mobile”的子文件夹。'images' 文件夹可以包含一个 'user_uploaded' 文件夹,然后可以包含''profile'。当然,您可以根据需要添加文件夹,在一个项目中,我有一个名为“uploaders”的文件夹,其中仅包含独立的上传脚本。
I also use convenience methods which help construct the filenames of what I want to load. For example, my loadView() will look for the view file in the current module directory, or if you pass an optional $module argument, it will look specifically within that module's folder.
我还使用方便的方法来帮助构建我想要加载的文件名。例如,我的 loadView() 将在当前模块目录中查找视图文件,或者如果您传递一个可选的 $module 参数,它将专门在该模块的文件夹中查找。
I hope this helps.
我希望这有帮助。
回答by OIS
You should have one directory as web root, where only files you want exposed to the whole internet should reside.
您应该有一个目录作为 Web 根目录,其中只有您希望向整个 Internet 公开的文件应驻留在该目录中。
project/
web/
index.php
css/
js/
images/
config/
lib/
- web/ is the root shown to visitors
- lib/ is here the library folder, and where autoload look for files.
- web/ 是向访问者显示的根
- lib/ 在这里是库文件夹,也是自动加载查找文件的地方。
You can add more subfolders to project/ like controller, modules, view, helper, etc. This depends on your framework.
您可以将更多子文件夹添加到项目/如控制器、模块、视图、助手等。这取决于您的框架。
EDIT:
编辑:
If you use composer (which I recommend) and maybe npm with grunt and less your file structure would be the following:
如果您使用composer(我推荐)并且可能使用grunt和更少的npm,您的文件结构将如下所示:
project/
web/
js/
css/
images/
index.php
cli/
config/
config.php
node_modules/
src/
test/
vendor/
composer.json
composer.lock
packages.json
- web/ has all your public files
- cli/ scripts and programs to be run from command line NOT the web
- config/ has all your config files (in git you ignore config.php and instead have config.dist.php without usernames, passwords, validation codes and table prefixes/suffixes and other "secrets")
- node_modules/ has all your library files from npm (in git I suggest you put this in a submodule)
- src has all your local PHP files in psr4 structure, set up to autoload in composer.json
- test/ has all your unit tests for your src classes, set up in autload-dev in composer.json (remember to use composer install --no-devon live, maybe add -oif you don't have too many classes)
- vendor has all your library files from composer and the ONE AND ONLY autoload.php to be included in web/index.php and any cli scripts (in git I suggest you ignore this vendor folder)
- web/ 拥有你所有的公共文件
- 要从命令行而不是 Web 运行的 cli/ 脚本和程序
- config/ 拥有你所有的配置文件(在 git 中你忽略 config.php 而是拥有 config.dist.php 没有用户名、密码、验证码和表前缀/后缀和其他“秘密”)
- node_modules/ 拥有来自 npm 的所有库文件(在 git 中我建议你把它放在一个子模块中)
- src 在 psr4 结构中有你所有的本地 PHP 文件,设置为在 composer.json 中自动加载
- test/ 为你的 src 类设置了所有单元测试,在 composer.json 中的 autload-dev 中设置(记得在 live 上使用composer install --no-dev,如果你没有太多类,可以添加-o)
- vendor 有来自 composer 的所有库文件,并且只有一个 autoload.php 包含在 web/index.php 和任何 cli 脚本中(在 git 中我建议你忽略这个 vendor 文件夹)
Add other folders and files as required for your project.
根据您的项目需要添加其他文件夹和文件。
For deployment use this structure:
对于部署使用此结构:
/sites/project/ (project is your projectname)
current (alias to current release folder releases/v1.1.0)
previous (optional alias to previous release folder releases/v1.0.1)
releases/
v1.0.0/ (git checkout of tag v1.0.0)
v1.0.1/ (git checkout of tag v1.0.1)
v1.1.0/ (git checkout of tag v1.1.0)
shared/ (has all your shared files and folders to be aliased in all releases - maybe something like GlusterFS)
Make a deployment script. Something like this:
制作部署脚本。像这样的东西:
First take backup of db or to copy it to a new database, checkout git repo to new folder with release tag, get all git submodules, run composer install --no-dev, setup any aliases for shared folders and files like uploaded images and configuration files, generate js/css with grunt and less or equivalent, point current alias to the new folder with the tag, run update database script, restart nginx/apache/fpm-php services, run tests to check the website is up.
首先备份 db 或将其复制到新数据库,将 git repo 签出到带有发布标记的新文件夹,获取所有 git 子模块,运行 composer install --no-dev,为共享文件夹和文件(如上传的图像)设置任何别名和配置文件,使用 grunt 和 less 或等效文件生成 js/css,将当前别名指向带有标签的新文件夹,运行更新数据库脚本,重新启动 nginx/apache/fpm-php 服务,运行测试以检查网站是否已启动。
Have a script to go back to previous version (or a guide so you know what to do).
有一个脚本可以返回到以前的版本(或一个指南,以便您知道该怎么做)。
回答by mauris
For core files which are included: approot/inc/
对于包含的核心文件:approot/inc/
For data access functions and classes are in: approot/dao/
数据访问函数和类在:approot/dao/
For javascripts: approot/scripts/
对于 javascripts:approot/scripts/
For CSS: approot/styles/
对于 CSS:approot/styles/
For images: approot/img/
对于图像:approot/img/
For static content (normally for user profile pictures or uploaded images): approot/static/
对于静态内容(通常用于用户个人资料图片或上传的图片):approot/static/
For caches: approot/caches/
对于缓存:approot/caches/
For templates or View files: approot/templates/
对于模板或查看文件:approot/templates/
All pages file: approot/
所有页面文件:approot/
Structure from Samstyle PHP Framework
The answer I posted here was from 2009. Over the years more standards were published, including PSR-0which covers the topic on folder structure. I also have a new (and I feel that it's better) folder structure with Packfire Framework.
我在这里发布的答案是 2009 年的。多年来,发布了更多标准,包括涵盖文件夹结构主题的PSR-0。我也有一个带有Packfire Framework的新(我觉得它更好)文件夹结构。
回答by Paul Tarjan
In my experience, you can never plan for this. You can try to follow what frameworks do, but I find I never quite fit exactly into their mold.
根据我的经验,您永远无法为此做计划。你可以尝试遵循框架的做法,但我发现我从来没有完全适应他们的模式。
I recommend to just keep a good rule of thumb for 20 files in a directory maximum. If you find you need more, just create a few sub directories and move common components in there.
我建议在一个目录中最多保留 20 个文件的良好经验法则。如果您发现需要更多,只需创建几个子目录并将常用组件移到其中即可。
回答by Imrul
I use codeigniter for small and big projects. It's MVC feature is moderately good.
我将 codeigniter 用于小型和大型项目。它的MVC特性还算不错。
- codeIgniter\system\application\config : contain all kind of configuration files like DB,Payment gateway, ftp config, routes and ...
- codeIgniter\system\application\models: contain all kinds of database classes, you should create sub folders according to your need, I used customers, mailData, paymentModel, report, web-service and ....
- codeIgniter\system\application\views: contain all kinds of files that will work as output for clients, you should think of reuse these files if possible. Like the models you had to create sub folder like administration, reports, email, email_template .....
- codeIgniter\system\application\controllers : this is the most important part. This will help to create SEO url, so you should be more careful about sub folders this time. You can create like administration, products, reports, orders..... and consider a good name for the functions of the controller class.
- codeIgniter\system\application\config :包含所有类型的配置文件,如 DB、支付网关、ftp 配置、路由和...
- codeIgniter\system\application\models:包含各种数据库类,根据需要创建子文件夹,我用过customers、mailData、paymentModel、report、web-service等等。
- codeIgniter\system\application\views:包含所有类型的文件,作为客户端的输出,如果可能,你应该考虑重用这些文件。就像模型一样,您必须创建子文件夹,如管理、报告、电子邮件、email_template .....
- codeIgniter\system\application\controllers :这是最重要的部分。这将有助于创建 SEO url,因此这次您应该更加注意子文件夹。您可以创建诸如管理、产品、报告、订单..... 并为控制器类的功能考虑一个好名字。
These were for the PHP/HTML file.
这些用于 PHP/HTML 文件。
Now about the other files:
现在关于其他文件:
- codeIgniter\images: for the images
- codeIgniter\scripts: for the Java scripts and their framework
- codeIgniter\styles: for the CSS
- codeIgniter\uploads: for the uploaded files, if you don't want to put files in the DB
- codeIgniter\images: 用于图像
- codeIgniter\scripts:用于 Java 脚本及其框架
- codeIgniter\styles: 用于 CSS
- codeIgniter\uploads:对于上传的文件,如果你不想把文件放在数据库中
For the detail see codeIgniter framework in detail.
有关详细信息,请参阅 codeIgniter 框架的详细信息。
Here "codeIgniter\" is the approot
这里“codeIgniter\”是approot
回答by Jefferson Lima
This is mostly a matter of preference, a quick Google search would reveal many different project structures. But it would be really nice if there were an agreed upon standard. I think this initiativeby the PHP Package Development Standards is a good candidate.
这主要是一个偏好问题,快速的谷歌搜索会发现许多不同的项目结构。但如果有一个商定的标准,那就太好了。我认为PHP Package Development Standards 的这个倡议是一个很好的候选者。
This is the directory structure they propose:
这是他们建议的目录结构:
- bin/: command-line executables
- config/: configuration files
- docs/: documentation files
- public/: web server files
- resources/: other resource files
- src/: PHP source code
- tests/: test code
- bin/: 命令行可执行文件
- config/: 配置文件
- docs/: 文档文件
- public/: 网络服务器文件
- resources/: 其他资源文件
- src/: PHP 源代码
- 测试/:测试代码
EDIT:
编辑:
This is also mentioned in the PHP The Right Wayunder the section Common Directory structure.
这也在PHP The Right Way部分的公共目录结构中提到。
回答by Az.Youness
This is the structure i'm using currently,
这是我目前使用的结构,
public/
assets/ /* js, css, imgs, ... */
index.php
src/
config/ /* for config files */
helpers/ /* for functions */
libraries/ /* for free classes that are not MVC classes */
models/ /* for M in MVC */
views/ /* for V in MVC */
controllers/ /* for C in MVC */
vendor/ /* for vendors files */
uploads/ /* for uploaded images, docs, ... */
回答by Ritesh Aryal
Even though the question is abit old, I still think it is wise to suggest the latest scaleable application structure which I have been working in my SOA based application and working absolutely fine.
即使这个问题有点老,我仍然认为建议最新的可扩展应用程序结构是明智的,我一直在我的基于 SOA 的应用程序中工作并且工作得非常好。
myApplication/
app/
config/
+ this can include custom MVC structure
cli/
docker/
lib/ - most commonly reusable components
logs/
public/ - should contain all publicly exposable web contains
sql/ - db migration stuffs
tests/ - compulsory test
tools/ - application addon tools like any kinds of rulset etc
vendor/
回答by antitoxic
Have a look at symfony 1.4or symfony 2dir structure. Choose what's most intuitive to you.
看看symfony 1.4或symfony 2目录结构。选择对您来说最直观的。
回答by heinkasner
I believe this depends on how large the project will become. This is what I used mostly:
我相信这取决于项目的规模。这是我主要使用的:
project/
index.php
img/
css/
js/
views/
functions/
项目/
index.php
img/
css/
js/
视图/
函数/
As long as all the project files are organised...
只要整理好所有的项目文件...

