Challenges building
complex applications
with Symfony2
Eduardo Oliveira
entering @ Github
Sorry no twitter
Slide 2
Slide 2 text
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.
…
…
Slide 3
Slide 3 text
Talk target
• Rich domain
• Complex applications
• Programmers that care about code
Slide 4
Slide 4 text
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)
Slide 5
Slide 5 text
About Symfony2
Slide 6
Slide 6 text
One of the most
popular frameworks
in the PHP community
Slide 7
Slide 7 text
Built on top of best
design patterns
Slide 8
Slide 8 text
A set of
decoupled and
standalone components
Slide 9
Slide 9 text
Is Symfony2 the
silver bullet?
Slide 10
Slide 10 text
Symfony2
is a good tool
Slide 11
Slide 11 text
Programmers that care
build good systems
not tools
Slide 12
Slide 12 text
A new Symfony2 project
Starts with the dream to write:
!
• elegant code
• code that you are proud
• reusable code
Slide 13
Slide 13 text
After some months…
Big Ball of Mud
Slide 14
Slide 14 text
Lets start…
Slide 15
Slide 15 text
No content
Slide 16
Slide 16 text
No content
Slide 17
Slide 17 text
Entity
Slide 18
Slide 18 text
Typical entity
Source: The Book for Symfony 2.4
Slide 19
Slide 19 text
Typical entity
• Thin
• No behavior
• Just a bag of getters and setters
Slide 20
Slide 20 text
Controller
Slide 21
Slide 21 text
Typical controller
Source: The Book for Symfony 2.4
Slide 22
Slide 22 text
Typical controller
• Fat
• Hidden dependencies
• Unit testing is a far away dream
Slide 23
Slide 23 text
Form
Slide 24
Slide 24 text
Typical form
Source: The Book for Symfony 2.4
Slide 25
Slide 25 text
Form binds to model entity
Source: The Book for Symfony 2.4
Slide 26
Slide 26 text
Typical validation
Source: The Book for Symfony 2.4
Slide 27
Slide 27 text
Typical form/entity
• High coupling (UI and domain)
• Mixed concerns (same validation
UI and domain)
• Domain entities can be constructed
in an invalid state
Slide 28
Slide 28 text
Repository
Slide 29
Slide 29 text
Typical repository interface
Source: The Book for Symfony 2.4
Slide 30
Slide 30 text
Typical repository
Source: The Book for Symfony 2.4
Slide 31
Slide 31 text
Typical use of repository
Source: The Book for Symfony 2.4
Slide 32
Slide 32 text
Typical repository
• Magic
• Domain coupled to
infrastructure, not to a contract
• No idea what queries your
application run
Slide 33
Slide 33 text
Is there
any hope?
!
Just if you deeply
care about your code
Slide 34
Slide 34 text
Entities & Doctrine
ORM
Slide 35
Slide 35 text
Entities & Doctrine ORM
• Data mapper (entity unaware of
persistence)
• No need for setters/getters, Doctrine
uses Reflection
• You own the object constructor
Slide 36
Slide 36 text
Entities & Doctrine ORM
Paradise?
Almost, but …
No VO’s support :(
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.
Slide 43
Slide 43 text
Controllers recipe
• Controllers as services
• Don’t use service locator
• Don’t extend the Symfony2 base
controller
• Group your dependencies and inject
them
Slide 44
Slide 44 text
Forms
Slide 45
Slide 45 text
Forms
• The Symfony2 component more loved/
hated
• Integrates well with validation
• You can control construction with
empty_data
• It requires getters
Slide 46
Slide 46 text
Forms
Source: http://williamdurand.fr/2013/12/16/enforcing-data-encapsulation-with-symfony-forms/
Slide 47
Slide 47 text
Forms
Source: https://twitter.com/couac/status/412677521977921536
Slide 48
Slide 48 text
Forms
Source: https://twitter.com/couac/status/412677521977921536
Slide 49
Slide 49 text
Forms
Source: https://twitter.com/couac/status/412677521977921536
Slide 50
Slide 50 text
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