'Project structure for PHP
I am new to PHP and want to know the directory structure for the php projects. I have experience in Java and in java we have src contains java source files, WEB-INF contains lib, and jsp pages. Do we have any similar standard directory structure in PHP? Also do we have layering in php like we have layers in java (e.g. Web, Service, DAO layers)
I browsed few links. But every one giving different answers.
Not sure if we can compare the two languages. I just want to stick to some standards.
Thanks in advance.
Solution 1:[1]
Nope. PHP is what you make of it. It can be very simple flat files, or however you want it.
That being said, there are a few agreed upon coding standards, but there is no "enforcement" of said standards. They are called PSR (PHP Standards Recommendation). There is a background on it here: http://net.tutsplus.com/tutorials/php/psr-huh/
You can view the standards one by one here: http://www.php-fig.org/psr/
Most major frameworks follow these standards, and if you are going to use one, it may be easier to go with the flow.
Again, every framework, project, plugin, program, etc, have different layouts with different project structures. A common structure is something like this:
-framework_dir
-public_html
-js
-img
-css
-index.php
-protected/private
-controllers
-models
-views
-etc
They then use the .htaccess
file to block access to the protected directories. Again, this is just the common representation I have seen in several frameworks. If you are doing a personal project, just use something that is comfortable to you. Every framework is going to give you a different library or way to access the data. There are no "layers", but again every framework has objects that handle different areas (email, database, cache, http, logs, etc). Because there are dozens of popular ones, it is just up to you to find what fits with your philosophy or project. Watch a few of the 5 minute blog videos, see what jives, and then give it a test run for a couple days. If you don't like it, switch to another.
Solution 2:[2]
With the invention of Composer, people now have a central place to register their projects for the world to consume, and other people now can look at that code base and see similarities.
The result is this: https://github.com/php-pds/skeleton
In short:
If a package has a root-level directory for | then it MUST be named |
---|---|
command-line executables | bin/ |
configuration files | config/ |
documentation files | docs/ |
web server files | public/ |
other resource files | resources/ |
PHP source code | src/ |
test code | tests/ |
This standard does not make any further recommendations about which directories have to exist below src
or public
.
The task to organize your PHP source files inside any of these directories is still up to you, but there are suggestions outlined in this article that I'd agree on: Either sort by type (controller, entity, service), or sort by feature (user, login, cart, catalog, article, comment) - the latter keeping all the code that belongs to one feature in on directory (or few sub directories), which often seems to be the better file organisation.
When organizing by type, you'll find yourself jumping between directories quite often, and also you do not get a good overview about what the code is about - you'd always have "Controller", but you rarely have "StampCollection".
Solution 3:[3]
I tend to use a Feature-based folder structure for my backend projects. Every feature-folder has his own controller, manager and routes file. This works well for api-backends. It looks in a way like https://blog.nikolaposa.in.rs/2017/01/16/on-structuring-php-projects/
For example, we have a Customer feature with a CustomerController, CustomerRepository, CustomerRoutes,..
My folder structure looks like this:
- build/
-- phpdox.xml
-- phpmd.xml
-- phpunit.dist.xml
- config/
- public/
-- .htaccess
-- index.php
-- assets/
- src/
-- Customer/
--- CustomerController.php
--- CustomerRepository.php
--- Customer.php
--- customer.routes.php
- tests/
- vendor/
composer.json
.gitignore
Solution 4:[4]
Unfortunately (or not?) you're very free with PHP. It's up to you.
Here's my structure:
framework/
controllers/
models/
configs/
files/
templates/
themes/
tmp/
index.php
init.php
.htaccess
You can control the access via .htaccess.
Solution 5:[5]
For a library, I am using the following structure... plus i've included recommendations I'm not using (yet)
PROJECT ROOT
|--composer.json
|--README.md
|--docs //for documentation files
|--tests //for Unit Tests
|--vendor //for external libraries (if everything isn't included through composer)
|--examples //examples of the library being used
|--config //any configuration files you may have
|--src //where the library's actual code "lives"
|--php //php source code, classes, any other scripts
|--View //html views, but actually php files that output html
|--Style //contains .css files
|--Script //contains .js files
|--Res //contains other deliverable resource files. Could be mp3 files, json etc
Currently, I'm only using composer.json
,README.md
, and src
among the root files. But I will probably use the others as I've described, when i get to that point.
By no means do I think this is "correct". And this setup only works because I have a php router on every request. With a .htaccess
, you could route a .css
file to /src/Style/requested_file.css
.
I wanted the project root to be cleaned up, and I have achieved that. PHP Fig does not have a PSR for directory structures... that I'm aware of. I had hoped PSR-4, autoloader would have had some standards, but... not really, in regard to directory structure.
You could look at laravel, wordpress, PHP Mailer and other php libraries to see examples and see what you might like best
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Withers |
Solution 2 | matiaslauriti |
Solution 3 | |
Solution 4 | Mr. B. |
Solution 5 | Reed |