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

后端 未结 3 929
情歌与酒
情歌与酒 2020-12-12 14:02

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 a

3条回答
  •  鱼传尺愫
    2020-12-12 14:44

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

提交回复
热议问题