Project structure for PHP

2020-05-17 05:10发布

问题:

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.

回答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.



回答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. I would suggest to simply have one namespace below src, and to implement any diversification of "models", "controllers" etc. by doing it via the fully qualified class name, e.g. if it is decided to have a Projectname\Controller\WhateverController class, it would reside in the PSR-4 compatible path src/Controller/WhateverController.php and be autoloaded via Composer with "autoload":{"psr-4":{"Projectname\\": "src"}}.



回答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


回答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.



回答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