PHP 的项目结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19648760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:49:42  来源:igfitidea点击:

Project structure for PHP

phpdirectorystructure

提问by javafan

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)

我是 PHP 新手,想知道 php 项目的目录结构。我有 Java 方面的经验,在 Java 中我们有 src 包含 java 源文件,WEB-INF 包含 lib 和 jsp 页面。我们在 PHP 中有任何类似的标准目录结构吗?我们在 php 中是否也有分层,就像我们在 java 中有层一样(例如 Web、服务、DAO 层)

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.

提前致谢。

回答by Tim Withers

Nope. PHP is what you make of it. It can be very simple flat files, or however you want it.

不。PHP 就是你所用的。它可以是非常简单的平面文件,也可以是您想要的任何文件。

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/

话虽如此,有一些就编码标准达成一致,但没有“强制执行”所述标准。它们被称为 PSR(PHP 标准建议)。这里有一个背景:http: //net.tutsplus.com/tutorials/php/psr-huh/
你可以在这里一一查看标准: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 .htaccessfile 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.

然后他们使用该.htaccess文件来阻止对受保护目录的访问。同样,这只是我在几个框架中看到的常见表示。如果你正在做一个个人项目,就使用你觉得舒服的东西。每个框架都会为您提供不同的库或访问数据的方式。没有“层”,但同样每个框架都有处理不同区域(电子邮件、数据库、缓存、http、日志等)的对象。因为有几十种流行的,所以你可以找到适合你的哲学或项目的东西。观看一些 5 分钟的博客视频,看看有什么好玩的,然后试运行几天。如果您不喜欢它,请切换到另一个。

回答by Sven

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.

随着 Composer 的发明,人们现在有了一个中心位置来注册他们的项目供全世界消费,其他人现在可以查看该代码库并看到相似之处。

The result is this: https://github.com/php-pds/skeleton

结果是这样的: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 srcor 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\WhateverControllerclass, it would reside in the PSR-4 compatible path src/Controller/WhateverController.phpand be autoloaded via Composer with "autoload":{"psr-4":{"Projectname\\": "src"}}.

本标准没有进一步建议哪些目录必须存在于srcpublic. 我建议在下面简单地有一个命名空间src,并通过完全限定的类名来实现“模型”、“控制器”等的任何多样化,例如,如果决定有一个Projectname\Controller\WhateverController类,它将驻留在PSR-4 兼容路径src/Controller/WhateverController.php并通过 Composer 自动加载"autoload":{"psr-4":{"Projectname\\": "src"}}.

回答by Gudgip

I tend to use a Feature-basedfolder 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/

我倾向于为我的后端项目使用基于功能的文件夹结构。每个功能文件夹都有自己的控制器、管理器和路由文件。这适用于api后端。它看起来像https://blog.nikolaposa.in.rs/2017/01/16/on-structuring-php-projects/

For example, we have a Customer feature with a CustomerController, CustomerRepository, CustomerRoutes,..

例如,我们有一个带有 CustomerController、CustomerRepository、CustomerRoutes 的 Customer 特性。

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

回答by Mr. B.

Unfortunately (or not?) you're very free with PHP. It's up to you.

不幸的是(或不是?)您对 PHP 非常自由。由你决定。

Here's my structure:

这是我的结构:

framework/
controllers/             
models/
configs/
files/
templates/
themes/
tmp/
index.php
init.php
.htaccess

You can control the access via .htaccess.

您可以通过 .htaccess 控制访问。

回答by Reed

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 srcamong the root files. But I will probably use the others as I've described, when i get to that point.

目前,我只使用composer.jsonREADME.mdsrc根目录的文件中。但是,当我到达那一点时,我可能会像我所描述的那样使用其他的。

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 .cssfile to /src/Style/requested_file.css.

我绝不认为这是“正确的”。并且此设置仅有效,因为我在每个请求上都有一个 php 路由器。使用.htaccess,您可以将.css文件路由到/src/Style/requested_file.css.

I wanted the project root to be cleaned up, and I have achieved that. PHP Figdoes not have a PSRfor directory structures... that I'm aware of. I had hoped PSR-4, autoloaderwould have had some standards, but... not really, in regard to directory structure.

我希望清理项目根目录,我已经实现了。PHP Fig没有目录结构的PSR......我知道。我曾希望PSR-4、自动加载器会有一些标准,但是……在目录结构方面并非如此。

You could look at laravel, wordpress, PHP Mailerand other php librariesto see examples and see what you might like best

您可以查看laravelwordpressPHP Mailer其他 php 库以查看示例并查看您最喜欢的内容