Best Practices in Symfony2

Best Practices in Symfony2

Given at International PHP Conference 2013

E9612cd342dbddff6640b99db21deee7?s=128

Andreas Hucks

June 04, 2013
Tweet

Transcript

  1. Best Practices in Symfony2 IPC Spring 2013 Berlin, June 4th

    Andreas Hucks
  2. @meandmymonkey Andreas Hucks Trainer & Consultant at SensioLabs Deutschland

  3. Best Practices

  4. Bad Practices

  5. symfony Day 2009

  6. Chapter One A Clean Project

  7. Naming Things in Symphony • Follow PSR-0, PSR-1, PSR-2 •

    Find a common scheme for your team • Be explicit • Be consistent
  8. Care about your coding style • Again - follow PSR-0,

    PSR-1, PSR-2 • Use PHPCSFixer http://goo.gl/tEK4y
  9. .gitignore /web/bundles/ /app/bootstrap.php.cache /app/cache/* /app/config/parameters.yml /app/logs/* /build/ /vendor/ /bin/ /composer.phar

    /data/uploads .idea add your own
  10. .gitignore /web/bundles/ /app/bootstrap.php.cache /app/cache/* /app/config/parameters.yml /app/logs/* /build/ /vendor/ /bin/ /composer.phar

    /data/uploads .idea should to be in your global .gitignore
  11. .gitignore /web/bundles/ /app/bootstrap.php.cache /app/cache/* /app/config/parameters.yml /app/logs/* /build/ /vendor/ /bin/ /composer.phar

    /data/uploads .idea maybe this too
  12. .gitignore /web/bundles/ /app/bootstrap.php.cache /app/cache/* /app/config/parameters.yml /app/logs/* /build/ /vendor/ /bin/ /composer.phar

    /data/uploads .idea this.
  13. Committing parameters.yml is a Three Kitten Offense

  14. Remove Acme\*

  15. Bundle Naming... Vendor\AwesomeBundle vs. Vendor\Bundle\AwesomeBundle

  16. Bundle Naming Vendor\AwesomeBundle vs. Vendor\Bundle\AwesomeBundle

  17. FAIL a.k.a. „because I can“ • MyCompleteAppBundle (ok for small

    projects) • MyAppNeedingGlobalResourcesBundle • MyBundleInsideAnotherBundleBundle
  18. What should go into a Bundle • Bundles should be

    self-contained • Sets of Features • Examples: Forum, AdminPanel... • Configured in /app/config
  19. Your config options • YAML • XML • Annotations •

    INI • PHP
  20. Your config options • YAML • XML • Annotations •

    INI • PHP meh.
  21. Your config options • YAML • XML • Annotations •

    INI • PHP special use cases
  22. Your config options • YAML • XML • Annotations •

    INI • PHP not for everything
  23. Your config options • YAML • XML • Annotations •

    INI • PHP Routing, Bundle Config, Parameters Services Validators, ORM/ODM
  24. Nest your routing files

  25. Chapter Two Controllers

  26. • Put Controllers on a Diet • Decouple them

  27. Try to lose the Boilerplate

  28. @Template([name])

  29. @Cache(...)

  30. Inject the Request into Controllers

  31. New in 2.3: handleRequest()

  32. Events for add-on actions

  33. Persistence Handlers

  34. Persistence Handlers

  35. Using the BaseController? Wrap calls to the Container

  36. Chapter Three Dependency Injection

  37. Injecting the Container

  38. But... but Symfony is doing it!

  39. ...\TwigBundle\Extension\AssetsExtension

  40. It‘s because of Scopes!

  41. Solution: Synchronized Services (>= 2.3)

  42. Alternative: Providing the Request (< 2.3)

  43. Alternative: Providing the Request (< 2.3)

  44. Service Organization

  45. Split up Service Definitions

  46. Dynamic Loading of Service Definitions

  47. Use Semantic Configuration!

  48. The Container as a Registry

  49. Instead: Proper Service Configuration

  50. Binding to the Environment

  51. Instead: Use Your config files

  52. Miscellaneous • Use XML for Service Definitions • Remember you

    can use Environment Variables (Apache, Nginx, ...) • Use %kernel.root_dir% as a reference
  53. Intermezzo Random Tips

  54. PHP • Use 5.4, it‘s faster • Use APC (or

    one of the alternatives)
  55. Composer • Use the --optimize-autoloader option

  56. Doctrine • Activate Metadata Cache • Activate Query Cache •

    Use factory-service to register Repositories & ObjectManagers as Services
  57. Security • Make sure there are no leaks in the

    security.yml access_control section! • Better: Check Authorization in Controller, possibly use SecurityExtraBundle
  58. Translation • Work with translation keys instead of full text

    to avoid breaking translations
  59. Searching • Look for „Symfony2“ (without the space)

  60. Read the Documentation (and the Changelogs)

  61. Chapter Four Forms

  62. Forms in Controllers

  63. • Couples Form setup to Controller • No reusability

  64. Better: Use Type Classes

  65. Always set the data_class

  66. Using Data in Constructors

  67. Use Form Events for Related Data

  68. Define Types as Services

  69. Define Types as Services

  70. Don‘t disable CSRF

  71. Thanks! Questions? Please give feedback: http://goo.gl/kwEqY