Symfony 4:如何组织文件夹结构(即您的业务逻辑) [英] Symfony 4: How to organize folder structure (namely, your business logic)

查看:26
本文介绍了Symfony 4:如何组织文件夹结构(即您的业务逻辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony 最佳实践中是建议不要使用 bundle 来组织业务逻辑.

In the Symfony Best Practices is advised to not use bundles to organize business logic.

仅当包中的代码要在其他应用程序中按原样重用时才应使用包:

The bundles should be used only when the code in them is meant to be reused as-is in other applications:

但是bundle 是指可以重用的东西独立的软件.如果 UserBundle 不能按原样"使用其他 Symfony 应用程序,那么它不应该是它自己的包.

But a bundle is meant to be something that can be reused as a stand-alone piece of software. If UserBundle cannot be used "as is" in other Symfony apps, then it shouldn't be its own bundle.

所以,当我将我的应用从 Symfony 3.3 升级到 Symfony 4 时,我认为现在是重新组织我的代码的合适时机.

So, as I'm upgrading my app from Symfony 3.3 to Symfony 4, I think this is the right time to reorganize my code.

目前我遵循了捆绑结构":

At the moment I have followed the "bundled-structure":

- src
   - AppBundle
      - Controller
      - Entity
      - Repository
      - Resources
      - ...
   - MyNamespace
      - Bundle
          - BundleTodo
              - Controller
              - Entity
              - Repository
              - Resources
              - ...
          - BundleCatalog
              - Controller
              - Entity
              - Repository
              - Resources
              - ...
          - BundleCart
              - Controller
              - Entity
              - Repository
              - Resources
              - ...
          - ...

现在,有了新的目录结构,我应该如何组织我的代码?

Now, with the new directory structure, how should have I to organize my code?

我想这样组织:

-src
   - Core
      - Controller
      - Entity
      - Repository
      - ..
   - Todos
      - Controller
      - Entity
      - Repository
      - ..
   - Catalog
      - Controller
      - Entity
      - Repository
      - ..
   - Cart
      - Controller
      - Entity
      - Repository
      - ...

但是,这是正确的吗?Symfony 4 和 Flex 的预期文件夹结构有问题吗?

But, is this correct? Is there any problem with the expected folder structure of Symfony 4 and Flex?

或者像这样更好:

-src
   - Controller
       - Core
       - Todos
       - Catalog
       - Cart
       - ...
   - Entity
       - Core
       - Todos
       - Catalog
       - Cart
       - ...
   - Repository
       - Core
       - Todos
       - Catalog
       - Cart
       - ...
   - ...

这同样适用于项目目录结构(关于如何覆盖它).

The same applies also to other root folders as described in the project directory structure (about how to override it).

在决定我的新文件夹结构时,是否需要考虑任何规则或限制?

尝试解决问题

所以,为了解决这个问题,我会更深入地阅读文档,我会在这里写下我会发现的内容.

So, trying to solve the problem, I'm going deeper in the documentation and I will write here what I will find.

推荐答案

正如评论中所述,Symfony 可以很好地处理所有这些结构,所以确实我们不能在这里有一个公认的 anwser,但这是我的两分钱.

As stated in comments, Symfony can work well with all these structures, so indeed we cannot have an accepted anwser here, but here is my two cents.

老实说,最佳实践是独立于框架来组织架构(主要是因为这个原因,Symfony 4 不再强加捆绑).

To be honest, the best practices would be to organize architecture independently of the framework (it is mainly for this reason that Symfony 4 no longer imposes a bundle).

但实际上,除了非常具体或复杂的项目,有一个面向symfony"的组织会更实际.

But in fact, except for really specific or complex projects, it will be more practical to have a "symfony-oriented" organization.

以下是我的个人喜好,也受到我项目类型的强烈影响(面向 CRUD,Rest API,没有强大的业务逻辑)

What follows is my personal preferences, and are also strongly influenced by the typology of my projects (CRUD oriented, Rest API, without strong business logic)

总的来说,我正在朝着这样的结构发展:

In general, I'm moving towards a structure like this:

-src
   - Controller
       - Core
       - Todos
       - ...
   - Entity
       - Core
       - Todos
       - ...
   - Repository
       - Core
       - Todos
   - Validator (or other Symfony oriented components)
       - Core
       - Todos
   - Others (depend on project)
       - Core
       - Todos
   - ...

原因是:

  • 使用自动装配减少服务定义 - 是的,我很懒;-)

  • Less service definition with autowire - yeah, I'm lazy ;-)

如果您需要将存储库或控制器注册为服务,只需一个声明即可.

If you need to register your repositories or controllers as services, you can do it with one declaration.

在 Symfony Flex 配方中,通常使用这种结构.

In Symfony Flex recipes, it is usually this structure that is used.

DoctrineBundle 例如初始化 src/Entitysrc/Repository 文件夹和包含实体的配方也使用这种结构.

DoctrineBundle for example initialize src/Entity and src/Repository folders and recipes that contains entities also use this structure.

但请记住,Symfony Flex 不是强制性的.其目的主要是为了简化项目的初始化,让框架更容易被初学者使用

这篇关于Symfony 4:如何组织文件夹结构(即您的业务逻辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆