[英]What is the most scalable PHP-based directory structure for a large site?
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? 您通常使用哪些目录结构,哪些目录结构在有数千个文件时最容易管理?
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: 这些文件夹都存在于我的主应用程序文件夹中:
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'.
例如,'css'文件夹可以包含名为'web'和'mobile'的子文件夹。 The 'images' folder could contain a 'user_uploaded' folder which could then contain`'profile'.
'images'文件夹可以包含'user_uploaded'文件夹,然后可以包含`'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.
当然,您可以根据需要添加文件夹,在一个项目中,我有一个名为“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. 我希望这有帮助。
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/
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: 如果您使用作曲家(我推荐),也许使用npm和grunt,你的文件结构将会更少:
project/
web/
js/
css/
images/
index.php
cli/
config/
config.php
node_modules/
src/
test/
vendor/
composer.json
composer.lock
packages.json
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复制到带有release标签的新文件夹,获取所有git子模块,运行composer install --no-dev,设置共享文件夹和上传图像等文件的任何别名配置文件,使用grunt生成js / css以及更少或等效,使用标记指向新文件夹的当前别名,运行更新数据库脚本,重新启动nginx / apache / fpm-php服务,运行测试以检查网站是否已启动。
Have a script to go back to previous version (or a guide so you know what to do). 有一个脚本可以回到以前的版本(或指南,所以你知道该怎么做)。
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 Samstyle PHP Framework的结构
The answer I posted here was from 2009. Over the years more standards were published, including PSR-0 which covers the topic on folder structure. 我在这里发布的答案来自2009年。多年来发布了更多标准,包括PSR-0 ,其中涵盖了文件夹结构的主题。 I also have a new (and I feel that it's better) folder structure with Packfire Framework .
我还有一个新的(我觉得它更好)文件夹结构与Packfire框架 。
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. 我建议在最大目录中保留20个文件的经验法则。 If you find you need more, just create a few sub directories and move common components in there.
如果您发现需要更多,只需创建一些子目录并在其中移动常用组件。
I use codeigniter for small and big projects. 我将codeigniter用于小型和大型项目。 It's MVC feature is moderately good.
它的MVC功能适度优秀。
These were for the PHP/HTML file. 这些是PHP / HTML文件。
Now about the other files: 现在关于其他文件:
For the detail see codeIgniter framework in detail. 有关详细信息,请参阅codeIgniter框架的详细信息。
Here "codeIgniter\\" is the approot 这里“codeIgniter”是批准
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 initiative by the PHP Package Development Standards is a good candidate.
我认为PHP Package Development Standards的这一举措是一个很好的选择。
This is the directory structure they propose: 这是他们提出的目录结构:
EDIT: 编辑:
This is also mentioned in the PHP The Right Way under the section Common Directory structure. 在公共目录结构部分的PHP正确方法中也提到了这一点。
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, ... */
I believe this depends on how large the project will become. 我相信这取决于项目的规模。 This is what I used mostly:
这是我主要使用的:
project/ 项目/
index.php 的index.php
img/ IMG /
css/ CSS /
js/ JS /
views/ 意见/
functions/ 职能/
As long as all the project files are organised... 只要所有项目文件都有条理......
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/
Have a look at symfony 1.4 or symfony 2 dir structure. 看看symfony 1.4或symfony 2 dir结构。 Choose what's most intuitive to you.
选择最直观的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.