Folder / file structure conventions?

I am trying to find a guide to PHP file and folder structure conventions.

I use GitHub and want me to abide by the standard agreement so as not to confuse users.

Any help would be appreciated.

+6
php
source share
5 answers

As speshak said, PHP has no standards and no conventions (including its own standard library).

But:

  • In the public directory (usually public_html ) only static resources (images / JS / CSS) and one PHP index.php file are stored, which is limited to the following:

     <?php require '/path/to/app/outside/public/html/start.php'; $app = new App(); $app->run(); 
  • The application itself must be stored outside the public directory.

  • The file structure may contain class names, therefore: Project\Util\XML\Parser will be stored in the file /path/to/the/project/Project/Util/XML/Parser.php .
  • Of course, third-party code can be stored in a separate folder, say vendor - this is a fairly common agreement.
+5
source share

PHP really has no standard. If you use any framework (for example, CakePHP, Zend Framework, etc.), it can impose a specific standard on you.

If you are not using a third-party library that forces the structure, just use common sense. (Put the images in the image directory, the included files in the include directory, etc.) The people who download and install the PHP applications will already be used for each application that performs different actions. The fact that you come up with it puts most of the competition in front of you :)

+3
source share

You can choose any directory structure. But if you want to learn about the best practices, see how this is done in the framework of, for example, a symphony.

Take a look: http://www.flickr.com/photos/deia/402335716/

Here are a few of them:

  • All code that is included must be placed outside the document root.

  • HTML templates should be in a separate directory.

  • Libraries and classes must be in the 'lib' directory.

These are mostly smart decisions, not tough agreements.

+3
source share

I think the most obvious is your libraries. You must name your classes as YourCompany_Module_Class if you want to be compatible. Using this standard, your libraries can be used together with any other similarly named libraries without collisions and problems. The new namespace in PHP 5.3+ helps to achieve this. You may have some recommendations for this in the Zend Coding Standards - File Naming and PSR-0 Standard Proposal .

In addition, you must constantly keep the future in your mind and plan your folder structure accordingly. For example, let's say you upload images to user_images . OK. But what happens when a project catches or gets bigger, and now you have tens of thousands of files in one folder. You need to create some scheme that allows you to store ~ ​​1k images in a directory larger than 12/56/154.jpg .

Over time, you will see many of these problems and opportunities. But you can watch current projects and learn from them for free :)

+3
source share

There are the following:

https://github.com/php-pds/skeleton

Based on data collected from various open source projects , this document proposes a naming convention for several directories at the root level for different general purposes, as well as naming conventions for documentation files at the root level.

I did not find that this covers all conceivable scenarios, but it's nice to be able to point it out and indicate that you are making efforts to achieve some coherence between the projects :-)

+2
source share

All Articles