Kwadz
Kwadz

Reputation: 2232

How to upgrade a Symfony 3 bundled application to a Symfony 4 bundle-less one?

At the section Upgrading Existing Applications to Flex, the Symfony 4 documentation states:

  1. Move the original source code from src/{App,...}Bundle/ to src/ and update the namespaces of every PHP file to be App\... (advanced IDEs can do this automatically).

The first part "Move the original source code from src/{App,...}Bundle/" is clear. We can understand a directory structure like this:

sf3-project/
├── src/
    ├── AppBundle/
        ├── Controller/
            ├── MyFirstController.php
            └── MySecondController.php
        └── ...
    └── ApiBundle/
        ├── Controller/
            ├── MyFirstController.php
            └── MySecondController.php
        └── ...

However, the second part "to src/ and update the namespaces of every PHP file to be App\..." is unclear to me, especially about the suggested structure.

As I understood, it suggests to copy the content of each Controller/ directory in one directory. If so, how to deal with controllers that have the same name? Should we add subdirectories with the prefix of the former bundle as name?

The symfony demo project seems to suggest it, because of the Admin/ directory under Controller/.

How should we upgrade the previous directory structure to the following?

sf4-project/
├── src/
    ├── Controller/
    │    └── ...
    ├── ...
    └── Kernel.php

We can notice this sf4-project example follows a package by layer structure, like in the documentation. I'm also wondering if we can easily use a package by feature structure.

So, what are the suggested ways with Symfony 4 for both package by layer and package by feature structures ?

Upvotes: 2

Views: 1224

Answers (1)

albert
albert

Reputation: 4468

Symfony and bundles

Symfony 2 mentioned "Bundles is a 1st class citizen in the symfony framework". So it was a bit opinionated framework where everything was a bundle even your app.

Later on the good practices recommended that a bundle should be only the reusable components of your app but we keep using bundles for everything to organize code.

This has created that lots of developers keep replicating the default bundle structure over and over ignoring what will work better for them. Just because it helps you to not have to think about architecture, you run a command and it generate all the folder structure and controllers.

If you have a look at the (well designed, compatible with other frameworks) symfony bundles you will find that they have at least 2 packages. One with the code, classes, services, etc and another package that configures all this inside symfony. Examples: Doctrine, jms/serializer, KnpMenu, etc

Bundles less symfony

I believe that the goal of moving to a bundle less symfony are:

  • Simple reusability with other frameworks: Laravel, Silex?, Zend
  • Make you think and design the best architecture for the project you are trying to solve

So moving away from the Symfony bundles to copy exactly what the new version of symfony has as a default/demo architecture will not help you at all.

My advice is: Forget the default/recommended setup of Symfony 4

Plan how you should organize your code: define your packages and libraries and where to put your code in order to keep it easy to develop, maintain and test.

Try to minimize your dependencies between packages and libraries.

Decide your own trade offs and then if you need to refactor do it.

If you are not fancy to plan your own architecture or you want to do it later do not refactor it yet.

Check Uncle Bob video about software architecture: https://www.youtube.com/watch?v=HhNIttd87xs

Upvotes: 1

Related Questions