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

Best Practices in Symfony2

Best Practices in Symfony2

Given at International PHP Conference 2013

Andreas Hucks

June 04, 2013
Tweet

More Decks by Andreas Hucks

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

  3. Best Practices

    View Slide

  4. Bad
    Practices

    View Slide

  5. symfony Day 2009

    View Slide

  6. Chapter One
    A Clean Project

    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

    View Slide

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

    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

    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 to be in your
    global .gitignore

    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
    maybe this too

    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.

    View Slide

  13. Committing parameters.yml is a
    Three Kitten Offense

    View Slide

  14. Remove Acme\*

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. Your config options
    • YAML
    • XML
    • Annotations
    • INI
    • PHP
    Routing, Bundle Config,
    Parameters
    Services
    Validators, ORM/ODM

    View Slide

  24. Nest your routing files

    View Slide

  25. Chapter Two
    Controllers

    View Slide

  26. • Put Controllers on a Diet
    • Decouple them

    View Slide

  27. Try to lose the Boilerplate

    View Slide

  28. @Template([name])

    View Slide

  29. @Cache(...)

    View Slide

  30. Inject the Request into Controllers

    View Slide

  31. New in 2.3: handleRequest()

    View Slide

  32. Events for add-on actions

    View Slide

  33. Persistence Handlers

    View Slide

  34. Persistence Handlers

    View Slide

  35. Using the BaseController?
    Wrap calls to the Container

    View Slide

  36. Chapter Three
    Dependency Injection

    View Slide

  37. Injecting the Container

    View Slide

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

    View Slide

  39. ...\TwigBundle\Extension\AssetsExtension

    View Slide

  40. It‘s because of Scopes!

    View Slide

  41. Solution: Synchronized Services (>= 2.3)

    View Slide

  42. Alternative: Providing the Request (< 2.3)

    View Slide

  43. Alternative: Providing the Request (< 2.3)

    View Slide

  44. Service Organization

    View Slide

  45. Split up Service Definitions

    View Slide

  46. Dynamic Loading of Service Definitions

    View Slide

  47. Use Semantic Configuration!

    View Slide

  48. The Container as a Registry

    View Slide

  49. Instead: Proper Service Configuration

    View Slide

  50. Binding to the Environment

    View Slide

  51. Instead: Use Your config files

    View Slide

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

    View Slide

  53. Intermezzo
    Random Tips

    View Slide

  54. PHP
    • Use 5.4, it‘s faster
    • Use APC (or one of the alternatives)

    View Slide

  55. Composer
    • Use the --optimize-autoloader option

    View Slide

  56. Doctrine
    • Activate Metadata Cache
    • Activate Query Cache
    • Use factory-service to register
    Repositories & ObjectManagers as
    Services

    View Slide

  57. Security
    • Make sure there are no leaks in the
    security.yml access_control section!
    • Better: Check Authorization in
    Controller, possibly use
    SecurityExtraBundle

    View Slide

  58. Translation
    • Work with translation keys instead of
    full text to avoid breaking translations

    View Slide

  59. Searching
    • Look for „Symfony2“
    (without the space)

    View Slide

  60. Read the Documentation
    (and the Changelogs)

    View Slide

  61. Chapter Four
    Forms

    View Slide

  62. Forms in Controllers

    View Slide

  63. • Couples Form setup to Controller
    • No reusability

    View Slide

  64. Better: Use Type Classes

    View Slide

  65. Always set the data_class

    View Slide

  66. Using Data in Constructors

    View Slide

  67. Use Form Events for Related Data

    View Slide

  68. Define Types as Services

    View Slide

  69. Define Types as Services

    View Slide

  70. Don‘t disable CSRF

    View Slide

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

    View Slide