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

42 best practices for Symfony2

42 best practices for Symfony2

English translation of the original french version (available at https://speakerdeck.com/tucksaun/42-bonnes-pratique-pour-symfony2)

Tugdual Saunier

April 05, 2013
Tweet

More Decks by Tugdual Saunier

Other Decks in Programming

Transcript

  1. DOCUMENTATION DOCUMENTATION When you are looking for documentation or seeking

    help. Use Symfony2 (capitalize, no space). There is so much resources available symfony (1.x) that filtering then out would make you lose time.
  2. BOOTSTRAPPING BOOTSTRAPPING Except if you are at ease and perfectly

    master Symfony2, Use the Standard Edition.
  3. BOOTSTRAPPING BOOTSTRAPPING Use the Standard Edition. But do some house

    cleaning! Delete AcmeDemoBundle Removed unused dependencies Change the favicon etc
  4. PROFILER PROFILER The Web Debug Toolbar gives you a lot

    of useful informations. Always keep an eye on it.
  5. PROFILER PROFILER Intercept redirection (in dev). This will help you

    during debugging and will help you to have a better view on the data collected by the profiler.
  6. YOUR CREDENTIALS ARE SECRET! YOUR CREDENTIALS ARE SECRET! Do not

    hardcode credentials in configuration: use parameters Do not version parameters.[ini|yml] Use environment variables
  7. YOUR CREDENTIALS ARE SECRET! YOUR CREDENTIALS ARE SECRET! # app/config/config.yml

    doctrine: dbal: username: %database_username% password: %database_password%
  8. YOUR CREDENTIALS ARE SECRET! YOUR CREDENTIALS ARE SECRET! # app/config/parameters.yml

    parameters: database_username: symfony database_password: s3cr3t
  9. YOUR CREDENTIALS ARE SECRET! YOUR CREDENTIALS ARE SECRET! <VirtualHost *:80>

    Servername www.domain.tld # ... SetEnv SYMFONY__DATABASE_USERNAME "symfony" SetEnv SYMFONY__DATABASE_PASSWORD "s3cr3t" </VirtualHost>
  10. YOUR CREDENTIALS ARE SECRET! YOUR CREDENTIALS ARE SECRET! This is

    not limited to passwords, apply for any “secret” information.
  11. CODING STYLE CODING STYLE A must-have tool is available to

    help your to respect Symfony’s coding style: PHP Coding Standards Fixer https://cs.symfony.com/ https://cs.symfony.com/
  12. APPLICATIONS? APPLICATION! APPLICATIONS? APPLICATION! By default, Symfony2 has a single

    application. It is quite unusual to need several ones, think thoroughly before implementing a multiple applications setup.
  13. APPLICATIONS APPLICATIONS If you really want to use several applications,

    your tree directory must be uniform and mimic as much as possible the Standard Edition one (app directory).
  14. BUNDLES BUNDLES One Bundle = One feature If your bundles

    are meant to be re-used and if your bundles are de-coupled. UserBundle => User Management ForumBundle => Forum ProductBundle => Product Management StoreBundle => E-Commerce
  15. BUNDLES & COMPONENTS/LIBRAIRIES BUNDLES & COMPONENTS/LIBRAIRIES In an ideal world,

    your bundles should only be the glue between Symfony2 and your business logic. The business logic must be independent. Take inspiration from the Bundles and Components organization in Symfony2.
  16. ROUTING ROUTING No route should be declared in app/config/routing.yml. This

    file should only contain imports. Route declarations should be stored in the bundle of the associated controller.
  17. FORMS FORMS After a successful form processing in POST, you

    must always redirect the user: security UX avoid double submission and data duplication
  18. COMPULSORY COMPULSORY No business logic in controllers. They must fetch

    services (or instantiate business objects), invoke them, and provide processing result to the view layer. That’s it.
  19. LENGTH LENGTH 20 lines max per action. More usually means

    they contain business logic. No more than 10 actions per controller. More than that and it becomes un-maintenable.
  20. LENGTH LENGTH Annotations can help to keep you controllers lighter

    and with less boilerplate. They also help you to centralize information and keep route declaration with the associated controller.
  21. FORMS FORMS Build your form in Form Types Reusable Do

    not clutter your controllers “Cleaner” organisation
  22. FORMS FORMS If your fom types have dependencies, inject them

    into the constructor, not using options.
  23. SESSIONS SESSIONS The session use logic must be in a

    service with the session injected. Not in controllers.
  24. LOGGING LOGGING The default Monolog configuration will only output debug

    logs if an error happens. Therefore do not refrain yourself to log.
  25. ORM ORM Use your entities to store information. Use dedicated

    business classes to manipulate them. “Treat your entities like princesses”. This eases unit testing, refactoring and bundles/component organisation.
  26. DIC DIC The DIC is without any doubts the most

    powerful tool in Symfony2. Symfony2 flexibility comes from the DIC. Read, learn, try!
  27. DIC DIC Do not hardcode paths. You can use the

    %kernel.root_dir% parameter or build paths during DIC compilation.
  28. BUNDLES BUNDLES You should not import your bundle’s configuration from

    app/config/config.yml. The bundle’s DI extension should take care of it.
  29. BUNDLES BUNDLES Validating this configuration let you: enable your services

    only if necessary, ensure your configuration is valid during warmup… and give explicit error messages to developer.
  30. FRONT CONTROLLER FRONT CONTROLLER Don’t deploy web/app_dev.php To be more

    accurate, the only front controller to deploy is web/app.php
  31. FORMS FORMS Go further, use the intention option in your

    forms. This will make the token unique for each form type.
  32. CACHING CACHING Page caching is simple with Symfony2. Do not

    generate the same content twice, use caching.
  33. PHP PHP Install and enable APC. You will save on

    time. (Should not be necessary anymore with PHP 5.5 😉 )
  34. Therefore, do not ask yourself “How to do this with

    Symfony2 ?”, but ask yourself “How to do this using PHP Object ?”.