Save 37% off PRO during our Black Friday Sale! »

Challenges building complex applications with Symfony2

Challenges building complex applications with Symfony2

Symfony2 ecosystem provides us with convenient ways of getting the job done. Over time conveniences like base controller, service locator, doctrine base repository and others promotes tight coupling becoming and obstacle in the development. We will focus on practical examples on how to make our code more domain focused and less dependent on Symfony2 ecosystem.


Eduardo Oliveira

May 15, 2014


  1. Challenges building complex applications with Symfony2 Eduardo Oliveira entering @

    Github Sorry no twitter
  2. Why I can talk about PHP and Symfony2? Note: The

    high number of deletes is due to delete PHPdoc generated docs. A MQ, no Symfony2 on this just PHP and Redis. … …
  3. Talk target • Rich domain • Complex applications • Programmers

    that care about code
  4. Doesn’t fit ! • CRUD applications • Simple applications, go

    with RAD approach instead • You are on it just for the money • Prototype (make sure you don’t ship it)
  5. About Symfony2

  6. One of the most popular frameworks in the PHP community

  7. Built on top of best design patterns

  8. A set of decoupled and standalone components

  9. Is Symfony2 the silver bullet?

  10. Symfony2 is a good tool

  11. Programmers that care build good systems not tools

  12. A new Symfony2 project Starts with the dream to write:

    ! • elegant code • code that you are proud • reusable code
  13. After some months… Big Ball of Mud

  14. Lets start…

  15. None
  16. None
  17. Entity

  18. Typical entity Source: The Book for Symfony 2.4

  19. Typical entity • Thin • No behavior • Just a

    bag of getters and setters
  20. Controller

  21. Typical controller Source: The Book for Symfony 2.4

  22. Typical controller • Fat • Hidden dependencies • Unit testing

    is a far away dream
  23. Form

  24. Typical form Source: The Book for Symfony 2.4

  25. Form binds to model entity Source: The Book for Symfony

  26. Typical validation Source: The Book for Symfony 2.4

  27. Typical form/entity • High coupling (UI and domain) • Mixed

    concerns (same validation UI and domain) • Domain entities can be constructed in an invalid state
  28. Repository

  29. Typical repository interface Source: The Book for Symfony 2.4

  30. Typical repository Source: The Book for Symfony 2.4

  31. Typical use of repository Source: The Book for Symfony 2.4

  32. Typical repository • Magic • Domain coupled to infrastructure, not

    to a contract • No idea what queries your application run
  33. Is there any hope? ! Just if you deeply care

    about your code
  34. Entities & Doctrine ORM

  35. Entities & Doctrine ORM • Data mapper (entity unaware of

    persistence) • No need for setters/getters, Doctrine uses Reflection • You own the object constructor
  36. Entities & Doctrine ORM Paradise? Almost, but … No VO’s

    support :(
  37. Entities & Doctrine ORM Sources:

  38. Entities & Doctrine ORM

  39. Entities & Doctrine ORM

  40. Entities & Doctrine ORM

  41. Entities & Doctrine ORM

  42. Entities & Doctrine ORM recipe Try VO’s support, if doesn’t

    fit in your use case don’t use Doctrine ORM 
 it will hurt too much your code. ! You can still and should use Doctrine DBAL.
  43. Controllers recipe • Controllers as services • Don’t use service

    locator • Don’t extend the Symfony2 base controller • Group your dependencies and inject them
  44. Forms

  45. Forms • The Symfony2 component more loved/ hated • Integrates

    well with validation • You can control construction with empty_data • It requires getters
  46. Forms Source:

  47. Forms Source:

  48. Forms Source:

  49. Forms Source:

  50. Forms recipe ! • Don’t bind forms to domain entities

    • Add specific entities to support the presentation layer • Use Symfony validations on presentation • Use Forms as services
  51. Repositories

  52. Repositories Source:

  53. Repositories recipe ! Don’t use the base Doctrine repository !

    Want to know more? repository
  54. Review