$30 off During Our Annual Pro Sale. View Details »

42 best practices for Symfony, a decade later

42 best practices for Symfony, a decade later

We are in March 2013. Symfony 2.2 has just been released, PHP 5.5 is around the corner and I give my first public talk during a SymfonyLive: “42 best practices for Symfony” (in French).
10+ years later a lot of things changed for Symfony, for PHP but also regarding development in general.

Let’s take a moment to look back at those best practices and see which one are still relevant, obsolete or now completely wrong. This would be a nice opportunity to reflect on the evolutions our ecosystem experienced over the last decade.

Tugdual Saunier

December 05, 2024
Tweet

More Decks by Tugdual Saunier

Other Decks in Programming

Transcript

  1. Tugdual Saunier / @tucksaun / [email protected] “No official best practices”

    Back in 2013, there was no official best practices list. This is not the case anymore: https://symfony.com/doc/ current/best_practices.html
  2. Tugdual Saunier / @tucksaun / [email protected] “Symfony2” No, not anymore

    😅 “Use Symfony2 (capitalized, no space) when you are looking for documentation or seeking help”
  3. Tugdual Saunier / @tucksaun / [email protected] Bootstrapping / Structure Well,

    well, well… Flex has been a game changer and the "skeleton" is now an empty shell. “Use the Standard Edition. But do some house cleaning!”
  4. Tugdual Saunier / @tucksaun / [email protected] Bootstrapping / Structure Also,

    there is now an official symfony tool that can be used to bootstrap a project symfony new
  5. Tugdual Saunier / @tucksaun / [email protected] Bootstrapping / Structure Does

    not even exist anymore 😂 “Don't deploy app_dev.php”
  6. Tugdual Saunier / @tucksaun / [email protected] Coding Style “A must-have

    tool is available to help your to respect Symfony's coding style: PHP Coding Standards Fixer (cs.symfony.com)” It's funny to see that this tool is still around!
  7. Tugdual Saunier / @tucksaun / [email protected] Semantic configuration “Use Semantic

    Configuration for your application. This is useful to validate configuration during cache warmup and display meaningful error messages.”
  8. Tugdual Saunier / @tucksaun / [email protected] Forms “Build your form

    in Form Types.” “Always redirect the user after a successful form processing in POST.”
  9. Tugdual Saunier / @tucksaun / [email protected] Doctrine “Queries must be

    created in dedicated classes (Repository), not directly in controllers/services.” “Use your entities to store data. Use dedicated business classes to manipulate them.” “Enable metadata and query caches.”
  10. Tugdual Saunier / @tucksaun / [email protected] Logging “Do not refrain

    yourself to log.” “Monolog's default configuration is really good for production.”
  11. Tugdual Saunier / @tucksaun / [email protected] Profiler “The Web Debug

    Toolbar gives you a lot of useful informations. Always keep an eye on it.”
  12. Tugdual Saunier / @tucksaun / [email protected] PHP “Use PHP 5.4”

    Time flies 😂 Should have been phrased as “use the latest available version” but the idea behind still stands
  13. Tugdual Saunier / @tucksaun / [email protected] Bundles Back in 2013,

    organising your code in Bundles was a challenge and the source of a lot of questioning. The tips evoked at this moment are still relevant but only for specific use cases.
  14. Tugdual Saunier / @tucksaun / [email protected] Dependency Injection Container (DIC)

    But today, the Autowiring changed the game and the pure DIC experience is mostly needed for advanced use cases “The DIC is without any doubts the most powerful tool in Symfony. Symfony flexibility comes from the DIC.”
  15. Tugdual Saunier / @tucksaun / [email protected] DIC The Autowiring makes

    your life easier. Use it! But look at what happens behind the scenes.
  16. Tugdual Saunier / @tucksaun / [email protected] Your credentials are sensitive!

    We now also have Secrets 🦹 Have a look at them • Do not hardcode credentials in configuration: use parameters • Do not version parameters.[ini|yml] • Use environment variables
  17. Tugdual Saunier / @tucksaun / [email protected] Views Twig is the

    only templating engine available natively today. So the recommendation to use it still stands, but you have less choice 😆 “Use Twig”
  18. Tugdual Saunier / @tucksaun / [email protected] Docker Use Docker (or

    a similar tech) in order to get a reproductible stack.
  19. Tugdual Saunier / @tucksaun / [email protected] Messenger Use Messenger early

    on (even for synchronous handling). Consider moving to async background handling.
  20. Tugdual Saunier / @tucksaun / [email protected] Scheduler Combine Console +

    Messenger + Scheduler This is a really powerful combo, your logic can then be easily triggered either: • from the web • on a regular basis • manually using the command line
  21. Tugdual Saunier / @tucksaun / [email protected] Embrace the small iterative

    changes Apply minor version upgrades early on! Symfony now has the BC promise. This means the minor version upgrades are safe. They will bring you new features without breaking changes.
  22. Tugdual Saunier / @tucksaun / [email protected] Prepare for bigger changes

    With minor version upgrades you will gain deprecations notices and BC layer. So you can migrate to new ways of doing things earlier and little by little
  23. Tugdual Saunier / @tucksaun / [email protected] Embrace the bigger changes

    For smooth major version upgrades: Monitor the deprecations reporting and clean them little by little Use Rector Have tests Follow best practices 🤓