简体   繁体   English

PHP 的项目结构

[英]Project structure for PHP

I am new to PHP and want to know the directory structure for the php projects.我是 PHP 新手,想知道 php 项目的目录结构。 I have experience in Java and in java we have src contains java source files, WEB-INF contains lib, and jsp pages.我有 Java 方面的经验,在 Java 中我们有 src 包含 java 源文件,WEB-INF 包含 lib 和 jsp 页面。 Do we have any similar standard directory structure in PHP?我们在 PHP 中有任何类似的标准目录结构吗? Also do we have layering in php like we have layers in java (eg Web, Service, DAO layers)我们在 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.提前致谢。

Nope.不。 PHP is what you make of it. PHP 就是你所用的。 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).它们被称为 PSR(PHP 标准建议)。 There is a background on it here: http://net.tutsplus.com/tutorials/php/psr-huh/这里有一个背景: http : //net.tutsplus.com/tutorials/php/psr-huh/
You can view the standards one by one here: http://www.php-fig.org/psr/你可以在这里一一查看标准: 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.然后他们使用.htaccess文件来阻止对受保护目录的访问。 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).没有“层”,但同样每个框架都有处理不同区域(电子邮件、数据库、缓存、http、日志等)的对象。 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.观看一些 5 分钟的博客视频,看看有什么好玩的,然后试运行几天。 If you don't like it, switch to another.如果您不喜欢它,请切换到另一个。

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 src or public .该标准没有进一步建议哪些目录必须存在于srcpublic之下。

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.在任何这些目录中组织 PHP 源文件的任务仍然取决于您,但本文中列出一些我同意的建议:按类型(控制器、实体、服务)排序,或按功能排序(用户、登录、购物车、目录、文章、评论)——后者将属于一个功能的所有代码保存在目录(或几个子目录)中,这通常似乎是更好的文件组织方式。

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".按类型组织时,您会发现自己经常在目录之间跳转,而且您无法很好地了解代码的内容——您总是有“控制器”,但很少有“StampCollection”。

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.这适用于api后端。 It looks in a way like https://blog.nikolaposa.in.rs/2017/01/16/on-structuring-php-projects/它看起来像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

Unfortunately (or not?) you're very free with PHP.不幸的是(或不是?)您对 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.您可以通过 .htaccess 控制访问。

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.目前,我只在根文件中使用composer.jsonREADME.mdsrc 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.并且此设置仅有效,因为我在每个请求上都有一个 php 路由器。 With a .htaccess , you could route a .css file to /src/Style/requested_file.css .使用.htaccess ,您可以将.css文件路由到/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. PHP Fig没有目录结构的PSR ......我知道。 I had hoped PSR-4, autoloader would have had some standards, but... not really, in regard to directory structure.我曾希望PSR-4、自动加载器会有一些标准,但是……在目录结构方面并非如此。

You could look at laravel , wordpress , PHP Mailer andother php libraries to see examples and see what you might like best您可以查看laravelwordpressPHP Mailer其他 php 库以查看示例并了解您最喜欢的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM