[ACCEPTED]-What is the most scalable PHP-based directory structure for a large site?-directory

Accepted answer
Score: 25

This is my setup. It's worked great for 20 me for small - very large projects (including 19 a social network).
These folders would all 18 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
  • views - contains views that should be globally accessible (page header, footer, etc)

All 17 the directories could obviously contain 16 sub-folders that would further organize 15 your files. For example, the 'css' folder 14 could have sub-folders named 'web' and 'mobile'. The 13 'images' folder could contain a 'user_uploaded' folder 12 which could then contain`'profile'. And 11 of course you can add folders as you see 10 fit, in one project I have a folder called 9 'uploaders' which just contains stand-alone 8 upload scripts.

I also use convenience 7 methods which help construct the filenames 6 of what I want to load. For example, my 5 loadView() will look for the view file in 4 the current module directory, or if you 3 pass an optional $module argument, it will 2 look specifically within that module's folder.

I 1 hope this helps.

Score: 23

You should have one directory as web root, where 22 only files you want exposed to the whole 21 internet should reside.

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.

You can add more 20 subfolders to project/ like controller, modules, view, helper, etc. This 19 depends on your framework.

EDIT:

If you use composer 18 (which I recommend) and maybe npm with grunt 17 and less your file structure would be the 16 following:

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-dev on live, maybe add -o if 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)

Add other folders and files as 15 required for your project.

For deployment 14 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 13 like this:

First take backup of db or to 12 copy it to a new database, checkout git 11 repo to new folder with release tag, get 10 all git submodules, run composer install 9 --no-dev, setup any aliases for shared folders 8 and files like uploaded images and configuration 7 files, generate js/css with grunt and less 6 or equivalent, point current alias to the 5 new folder with the tag, run update database 4 script, restart nginx/apache/fpm-php services, run 3 tests to check the website is up.

Have a 2 script to go back to previous version (or 1 a guide so you know what to do).

Score: 5

For core files which are included: approot/inc/

For 12 data access functions and classes are in: approot/dao/

For 11 javascripts: approot/scripts/

For CSS: approot/styles/

For 10 images: approot/img/

For static content (normally 9 for user profile pictures or uploaded images): approot/static/

For 8 caches: approot/caches/

For templates or 7 View files: approot/templates/

All pages 6 file: approot/

Structure from Samstyle PHP Framework


The answer 5 I posted here was from 2009. Over the years 4 more standards were published, including 3 PSR-0 which covers the topic on folder structure. I 2 also have a new (and I feel that it's better) folder 1 structure with Packfire Framework.

Score: 4

In my experience, you can never plan for 7 this. You can try to follow what frameworks 6 do, but I find I never quite fit exactly 5 into their mold.

I recommend to just keep 4 a good rule of thumb for 20 files in a directory 3 maximum. If you find you need more, just 2 create a few sub directories and move common 1 components in there.

Score: 3

This is mostly a matter of preference, a 8 quick Google search would reveal many different 7 project structures. But it would be really 6 nice if there were an agreed upon standard. I 5 think this initiative by the PHP Package Development 4 Standards is a good candidate.

This is the 3 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

EDIT:

This 2 is also mentioned in the PHP The Right Way under the section 1 Common Directory structure.

Score: 2

I use codeigniter for small and big projects. It's 4 MVC feature is moderately good.

  • 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.

These were 3 for the PHP/HTML file.

Now about the other 2 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

For the detail see codeIgniter framework 1 in detail.

Here "codeIgniter\" is the approot

Score: 1

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, ... */

0

Score: 0

Have a look at symfony 1.4 or symfony 2 dir structure. Choose 1 what's most intuitive to you.

Score: 0

I believe this depends on how large the 3 project will become. This is what I used 2 mostly:

project/
  index.php
  img/
  css/
  js/
  views/
  functions/

As 1 long as all the project files are organised...

Score: 0

Even though the question is abit old, I 4 still think it is wise to suggest the latest 3 scaleable application structure which I 2 have been working in my SOA based application 1 and working absolutely fine.

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/

More Related questions