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

Best Practices in Symfony2

Best Practices in Symfony2

Andreas Hucks

October 09, 2013
Tweet

More Decks by Andreas Hucks

Other Decks in Programming

Transcript

  1. Best Practices in Symfony2
    phpnw 2013 Manchester, Oct. 6th
    Andreas Hucks
    Sonntag, 6. Oktober 13

    View Slide

  2. @meandmymonkey
    Andreas Hucks
    Trainer & Consultant at
    Sonntag, 6. Oktober 13

    View Slide

  3. Best Practices
    Sonntag, 6. Oktober 13

    View Slide

  4. Bad
    Practices
    Sonntag, 6. Oktober 13

    View Slide

  5. symfony Day 2009
    Sonntag, 6. Oktober 13

    View Slide

  6. Chapter One
    A Clean Project
    Sonntag, 6. Oktober 13

    View Slide

  7. Naming Things in Symphony
    • Follow PSR-0, PSR-1, PSR-2
    • Find a common scheme for your team
    • Be explicit
    • Be consistent
    Sonntag, 6. Oktober 13

    View Slide

  8. Care about your coding style
    • Again - follow PSR-0, PSR-1, PSR-2
    • Use PHPCSFixer
    http://goo.gl/tEK4y
    Sonntag, 6. Oktober 13

    View Slide

  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
    Sonntag, 6. Oktober 13

    View Slide

  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 be in your
    global .gitignore
    Sonntag, 6. Oktober 13

    View Slide

  11. .gitignore
    /web/bundles/
    /app/bootstrap.php.cache
    /app/cache/*
    /app/config/parameters.yml
    /app/logs/*
    /build/
    /vendor/
    /bin/
    /composer.phar
    /data/uploads
    .idea
    probably this too
    Sonntag, 6. Oktober 13

    View Slide

  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.
    Sonntag, 6. Oktober 13

    View Slide

  13. Committing parameters.yml is a
    Three Kitten Offense
    Sonntag, 6. Oktober 13

    View Slide

  14. Committing parameters.yml is a
    Three Kitten Offense
    Sonntag, 6. Oktober 13

    View Slide

  15. Remove Acme\*
    Sonntag, 6. Oktober 13

    View Slide

  16. Bundle Naming...
    Vendor\AwesomeBundle
    vs.
    Vendor\Bundle\AwesomeBundle
    Sonntag, 6. Oktober 13

    View Slide

  17. Bundle Naming...
    Vendor\AwesomeBundle
    vs.
    Vendor\Bundle\AwesomeBundle
    Sonntag, 6. Oktober 13

    View Slide

  18. FAIL a.k.a. „because I can“
    • MyCompleteAppBundle (ok for small projects)
    • MyAppNeedingGlobalResourcesBundle
    • MyBundleInsideAnotherBundleBundle
    Sonntag, 6. Oktober 13

    View Slide

  19. What should go into a Bundle
    • Bundles should be self-contained
    • Sets of Features
    • Examples: Forum, AdminPanel...
    • Configured in /app/config
    Sonntag, 6. Oktober 13

    View Slide

  20. Your config options
    • YAML
    • XML
    • Annotations
    • INI
    • PHP
    Sonntag, 6. Oktober 13

    View Slide

  21. Your config options
    • YAML
    • XML
    • Annotations
    • INI
    • PHP
    meh.
    Sonntag, 6. Oktober 13

    View Slide

  22. Your config options
    • YAML
    • XML
    • Annotations
    • INI
    • PHP
    special use cases
    Sonntag, 6. Oktober 13

    View Slide

  23. Your config options
    • YAML
    • XML
    • Annotations
    • INI
    • PHP
    not for everything
    Sonntag, 6. Oktober 13

    View Slide

  24. Your config options
    • YAML
    • XML
    • Annotations
    • INI
    • PHP
    Routing, Bundle Config,
    Parameters
    Services
    Validators, ORM/ODM
    Sonntag, 6. Oktober 13

    View Slide

  25. Nest your routing files
    Sonntag, 6. Oktober 13

    View Slide

  26. Chapter Two
    Controllers
    Sonntag, 6. Oktober 13

    View Slide

  27. • Put Controllers on a Diet
    • Decouple them
    Sonntag, 6. Oktober 13

    View Slide

  28. Try to lose the Boilerplate
    Sonntag, 6. Oktober 13

    View Slide

  29. @Template([name])
    Sonntag, 6. Oktober 13

    View Slide

  30. @Cache(...)
    Sonntag, 6. Oktober 13

    View Slide

  31. Inject the Request into Controllers
    Sonntag, 6. Oktober 13

    View Slide

  32. New in 2.3: handleRequest()
    Sonntag, 6. Oktober 13

    View Slide

  33. Events for add-on actions
    Sonntag, 6. Oktober 13

    View Slide

  34. Service Layers
    Sonntag, 6. Oktober 13

    View Slide

  35. Service Layers
    Sonntag, 6. Oktober 13

    View Slide

  36. Using the BaseController?
    Wrap calls to the Container
    Sonntag, 6. Oktober 13

    View Slide

  37. Chapter Three
    Dependency Injection
    Sonntag, 6. Oktober 13

    View Slide

  38. Injecting the Container
    Sonntag, 6. Oktober 13

    View Slide

  39. Injecting the Container
    Sonntag, 6. Oktober 13

    View Slide

  40. But... but Symfony is doing it!
    Sonntag, 6. Oktober 13

    View Slide

  41. ...\TwigBundle\Extension\AssetsExtension
    Sonntag, 6. Oktober 13

    View Slide

  42. It‘s because of Scopes!
    Sonntag, 6. Oktober 13

    View Slide

  43. Solution: Synchronized Services (>= 2.3)
    Sonntag, 6. Oktober 13

    View Slide

  44. Alternative: Providing the Request (< 2.3)
    Sonntag, 6. Oktober 13

    View Slide

  45. Alternative: Providing the Request (< 2.3)
    Sonntag, 6. Oktober 13

    View Slide

  46. Service Organization
    Sonntag, 6. Oktober 13

    View Slide

  47. Split up Service Definitions
    Sonntag, 6. Oktober 13

    View Slide

  48. Dynamic Loading of Service Definitions
    Sonntag, 6. Oktober 13

    View Slide

  49. Use Semantic Configuration!
    Sonntag, 6. Oktober 13

    View Slide

  50. The Container as a Registry
    Sonntag, 6. Oktober 13

    View Slide

  51. The Container as a Registry
    Sonntag, 6. Oktober 13

    View Slide

  52. Instead: Proper Service Configuration
    Sonntag, 6. Oktober 13

    View Slide

  53. Binding to the Environment
    Sonntag, 6. Oktober 13

    View Slide

  54. Binding to the Environment
    Sonntag, 6. Oktober 13

    View Slide

  55. Instead: Use Your config files
    Sonntag, 6. Oktober 13

    View Slide

  56. Miscellaneous
    • Use XML for Service Definitions
    • Remember you can use Environment
    Variables (Apache, Nginx, ...)
    • Use %kernel.root_dir% as a reference
    Sonntag, 6. Oktober 13

    View Slide

  57. Intermezzo
    Random Tips
    Sonntag, 6. Oktober 13

    View Slide

  58. PHP
    • Use 5.4/5.5, it‘s faster
    • Use APC (or one of the alternatives)
    Sonntag, 6. Oktober 13

    View Slide

  59. Composer
    • Use the --optimize-autoloader option
    Sonntag, 6. Oktober 13

    View Slide

  60. Doctrine
    • Activate Metadata Cache
    • Activate Query Cache
    • Use factory-service to register
    Repositories & ObjectManagers as
    Services
    • Do NOT inject the EntityManager into
    your entities
    Sonntag, 6. Oktober 13

    View Slide

  61. Security
    • Make sure there are no leaks in the
    security.yml access_control section!
    • Better: Check Authorization in
    Controller, possibly use
    JMSSecurityExtraBundle
    Sonntag, 6. Oktober 13

    View Slide

  62. Translation
    • Work with translation keys instead of
    full text to avoid breaking translations
    Sonntag, 6. Oktober 13

    View Slide

  63. Searching
    • Look for „Symfony2“
    (without the space)
    Sonntag, 6. Oktober 13

    View Slide

  64. Read the Documentation
    (and the Changelogs)
    Sonntag, 6. Oktober 13

    View Slide

  65. Chapter Four
    Forms
    Sonntag, 6. Oktober 13

    View Slide

  66. Forms in Controllers
    Sonntag, 6. Oktober 13

    View Slide

  67. Forms in Controllers
    Sonntag, 6. Oktober 13

    View Slide

  68. • Couples Form setup to Controller
    • No reusability
    Sonntag, 6. Oktober 13

    View Slide

  69. Better: Use Type Classes
    Sonntag, 6. Oktober 13

    View Slide

  70. Always set the data_class
    Sonntag, 6. Oktober 13

    View Slide

  71. Using Data in Constructors
    Sonntag, 6. Oktober 13

    View Slide

  72. Using Data in Constructors
    Sonntag, 6. Oktober 13

    View Slide

  73. Use Form Events for Related Data
    Sonntag, 6. Oktober 13

    View Slide

  74. Define Types as Services
    Sonntag, 6. Oktober 13

    View Slide

  75. Define Types as Services
    Sonntag, 6. Oktober 13

    View Slide

  76. Don‘t disable CSRF
    Sonntag, 6. Oktober 13

    View Slide

  77. Don‘t disable CSRF
    Sonntag, 6. Oktober 13

    View Slide

  78. Thanks! Questions?
    Please give feedback:
    http://goo.gl/yK97XK
    Sonntag, 6. Oktober 13

    View Slide