简体   繁体   English

什么是大型站点最可扩展的基于PHP的目录结构?

[英]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: 这些文件夹都存在于我的主应用程序文件夹中:

  • config - contains custom PHP config files config - 包含自定义PHP配置文件
  • css - contains the project's CSS files css - 包含项目的CSS文件
  • helpers - contains 'helper' files (each file is a collection of functions) helpers - 包含'helper'文件(每个文件是函数的集合)
  • images - contains the project's images 图像 - 包含项目的图像
  • js - contains the project's Javascript files js - 包含项目的Javascript文件
  • lib - contains PHP classes specific to the project lib - 包含特定于项目的PHP类
  • modules - My MVC framework allows packaging site sections as modules 模块 - 我的MVC框架允许将站点部分打包为模块
    • blog - An example module 博客 - 示例模块
      • controllers - contains the controllers for the module 控制器 - 包含模块的控制器
      • models - contains the models for the module models - 包含模块的模型
      • views - contains the views for the module views - 包含模块的视图
  • views - contains views that should be globally accessible (page header, footer, etc) views - 包含应该可全局访问的视图(页眉,页脚等)

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/
  • web/ is the root shown to visitors web /是向访问者显示的根
  • lib/ is here the library folder, and where autoload look for files. lib /是这里的库文件夹,以及autoload查找文件的位置。

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
  • web/ has all your public files web /包含所有公共文件
  • cli/ scripts and programs to be run from command line NOT the web cli /脚本和程序从命令行而不是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") config /拥有你所有的配置文件(在git中你忽略了config.php而不是没有用户名,密码,验证码和表前缀/后缀以及其他“秘密”的config.dist.php)
  • node_modules/ has all your library files from npm (in git I suggest you put this in a submodule) node_modules /拥有来自npm的所有库文件(在git中我建议你把它放在子模块中)
  • src has all your local PHP files in psr4 structure, set up to autoload in composer.json src包含psr4结构中的所有本地PHP文件,在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-dev on live, maybe add -o if you don't have too many classes) test /拥有你的src类的所有单元测试,在composer.json中的autload-dev中设置(记得在live上使用composer install --no-dev ,如果你没有太多类,可以添加-o
  • 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) 供应商拥有来自composer的所有库文件和ONE AND ONLY autoload.php将包含在web / index.php和任何cli脚本中(在git中我建议您忽略此供应商文件夹)

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功能适度优秀。

  • codeIgniter\\system\\application\\config : contain all kind of configuration files like DB,Payment gateway, ftp config, routes and ... codeIgniter \\ system \\ application \\ config:包含所有类型的配置文件,如DB,支付网关,ftp配置,路由和...
  • 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 \\ models:包含各种数据库类,你应该根据需要创建子文件夹,我用过customer,mailData,paymentModel,report,web-service和....
  • 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. codeIgniter \\ system \\ application \\ views:包含将作为客户端输出的各种文件,如果可能,您应该考虑重用这些文件。 Like the models you had to create sub folder like administration, reports, email, email_template ..... 像模型一样,您必须创建子文件夹,如管理,报告,电子邮件,email_template .....
  • codeIgniter\\system\\application\\controllers : this is the most important part. codeIgniter \\ system \\ application \\ controllers:这是最重要的部分。 This will help to create SEO url, so you should be more careful about sub folders this time. 这将有助于创建SEO网址,所以这次你应该更加小心子文件夹。 You can create like administration, products, reports, orders..... and consider a good name for the functions of the controller class. 您可以创建类似管理,产品,报告,订单......并考虑控制器类功能的良好名称。

These were for the PHP/HTML file. 这些是PHP / HTML文件。

Now about the other files: 现在关于其他文件:

  • codeIgniter\\images: for the images codeIgniter \\ images:用于图像
  • codeIgniter\\scripts: for the Java scripts and their framework codeIgniter \\ scripts:用于Java脚本及其框架
  • codeIgniter\\styles: for the CSS codeIgniter \\ styles:用于CSS
  • codeIgniter\\uploads: for the uploaded files, if you don't want to put files in the DB codeIgniter \\ uploads:对于上传的文件,如果您不想将文件放入数据库中

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: 这是他们提出的目录结构:

  • bin/ : command-line executables bin / :命令行可执行文件
  • config/ : configuration files config / :配置文件
  • docs/ : documentation files docs :文档文件
  • public/ : web server files public / :Web服务器文件
  • resources/ : other resource files resources / :其他资源文件
  • src/ : PHP source code src / :PHP源代码
  • tests/ : test code tests / :测试代码

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.4symfony 2 dir结构。 Choose what's most intuitive to you. 选择最直观的选择。

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

相关问题 在基于PHP的网站上基于电报的聊天:如何? - Telegram-based chat on a PHP-based site: HOWTO? 登录基于PHP的网站并抓取数据-问题 - Log into php-based site and scraping data - problems 使用MVC方法(基于PHP)的Web应用程序是否必然意味着它会比其他方法更具可伸缩性? - Does using an MVC approach to (PHP-based) web applications necessarily mean that it will be more scalable than other approaches? 基于PHP的搜索框架 - PHP-based search frameworks 应该在基于 PHP 的 Docker AWS lambda 的容器中运行什么? - What should be run in a container for a PHP-based Docker AWS lambda? 我可以将完整的MySQL DB从基于PHP的站点移动到Django应用程序吗? - Can I move complete MySQL DB from PHP-based site to a Django application? 我可以在基于PHP的网站上使用npm安装Fabric.js吗? - Can I install Fabric.js using npm for a PHP-based site? 基于PHP的图形脚本推荐 - PHP-based graphing script recommendation 阻止人们入侵 Flash 游戏的基于 PHP 的高分表的最佳方法是什么? - What is the best way to stop people hacking the PHP-based highscore table of a Flash game 基于PHP的Author for beardbeard,sab,maraschino - PHP-based Auth for sickbeard, sab, maraschino
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM