Symfony 4: How to organize folder structure (namely, your business logic)

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

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:

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.

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?

I'd like to organize it this way:

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

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

Or is better something like this:

-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).

Are there any rules or constraints that I have to take into account deciding my new folder structure?

TRYING TO SOLVE THE PROBLEM

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


回答1:

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.

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).

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

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

The reasons are:

  • 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.

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

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

But keep in minde that Symfony Flex is not mandatory. Its purpose is mainly to ease the project's init and make the framework more accessible to beginer

For a huge application with complex layers of logic business, it will be necessary to privilege a custom architecture.

The gain will be much more important in the long term than these choices which are deliberately close to the framework and where we finally find a lot of the old bundle convention.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!