Best Practices in Symfony2
phpnw 2013 Manchester, Oct. 6th
Andreas Hucks
Sonntag, 6. Oktober 13
Slide 2
Slide 2 text
@meandmymonkey
Andreas Hucks
Trainer & Consultant at
Sonntag, 6. Oktober 13
Slide 3
Slide 3 text
Best Practices
Sonntag, 6. Oktober 13
Slide 4
Slide 4 text
Bad
Practices
Sonntag, 6. Oktober 13
Slide 5
Slide 5 text
symfony Day 2009
Sonntag, 6. Oktober 13
Slide 6
Slide 6 text
Chapter One
A Clean Project
Sonntag, 6. Oktober 13
Slide 7
Slide 7 text
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
Slide 8
Slide 8 text
Care about your coding style
• Again - follow PSR-0, PSR-1, PSR-2
• Use PHPCSFixer
http://goo.gl/tEK4y
Sonntag, 6. Oktober 13
Slide 9
Slide 9 text
.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
Slide 10
Slide 10 text
.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
Slide 11
Slide 11 text
.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
Committing parameters.yml is a
Three Kitten Offense
Sonntag, 6. Oktober 13
Slide 14
Slide 14 text
Committing parameters.yml is a
Three Kitten Offense
Sonntag, 6. Oktober 13
Slide 15
Slide 15 text
Remove Acme\*
Sonntag, 6. Oktober 13
Slide 16
Slide 16 text
Bundle Naming...
Vendor\AwesomeBundle
vs.
Vendor\Bundle\AwesomeBundle
Sonntag, 6. Oktober 13
Slide 17
Slide 17 text
Bundle Naming...
Vendor\AwesomeBundle
vs.
Vendor\Bundle\AwesomeBundle
Sonntag, 6. Oktober 13
Slide 18
Slide 18 text
FAIL a.k.a. „because I can“
• MyCompleteAppBundle (ok for small projects)
• MyAppNeedingGlobalResourcesBundle
• MyBundleInsideAnotherBundleBundle
Sonntag, 6. Oktober 13
Slide 19
Slide 19 text
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
Slide 20
Slide 20 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
Sonntag, 6. Oktober 13
Slide 21
Slide 21 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
meh.
Sonntag, 6. Oktober 13
Slide 22
Slide 22 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
special use cases
Sonntag, 6. Oktober 13
Slide 23
Slide 23 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
not for everything
Sonntag, 6. Oktober 13
Slide 24
Slide 24 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
Routing, Bundle Config,
Parameters
Services
Validators, ORM/ODM
Sonntag, 6. Oktober 13
Slide 25
Slide 25 text
Nest your routing files
Sonntag, 6. Oktober 13
Slide 26
Slide 26 text
Chapter Two
Controllers
Sonntag, 6. Oktober 13
Slide 27
Slide 27 text
• Put Controllers on a Diet
• Decouple them
Sonntag, 6. Oktober 13
Slide 28
Slide 28 text
Try to lose the Boilerplate
Sonntag, 6. Oktober 13
Slide 29
Slide 29 text
@Template([name])
Sonntag, 6. Oktober 13
Slide 30
Slide 30 text
@Cache(...)
Sonntag, 6. Oktober 13
Slide 31
Slide 31 text
Inject the Request into Controllers
Sonntag, 6. Oktober 13
Slide 32
Slide 32 text
New in 2.3: handleRequest()
Sonntag, 6. Oktober 13
Slide 33
Slide 33 text
Events for add-on actions
Sonntag, 6. Oktober 13
Slide 34
Slide 34 text
Service Layers
Sonntag, 6. Oktober 13
Slide 35
Slide 35 text
Service Layers
Sonntag, 6. Oktober 13
Slide 36
Slide 36 text
Using the BaseController?
Wrap calls to the Container
Sonntag, 6. Oktober 13
Slide 37
Slide 37 text
Chapter Three
Dependency Injection
Sonntag, 6. Oktober 13
Slide 38
Slide 38 text
Injecting the Container
Sonntag, 6. Oktober 13
Slide 39
Slide 39 text
Injecting the Container
Sonntag, 6. Oktober 13
Slide 40
Slide 40 text
But... but Symfony is doing it!
Sonntag, 6. Oktober 13
Slide 41
Slide 41 text
...\TwigBundle\Extension\AssetsExtension
Sonntag, 6. Oktober 13
Slide 42
Slide 42 text
It‘s because of Scopes!
Sonntag, 6. Oktober 13
Slide 43
Slide 43 text
Solution: Synchronized Services (>= 2.3)
Sonntag, 6. Oktober 13
Slide 44
Slide 44 text
Alternative: Providing the Request (< 2.3)
Sonntag, 6. Oktober 13
Slide 45
Slide 45 text
Alternative: Providing the Request (< 2.3)
Sonntag, 6. Oktober 13
Slide 46
Slide 46 text
Service Organization
Sonntag, 6. Oktober 13
Slide 47
Slide 47 text
Split up Service Definitions
Sonntag, 6. Oktober 13
Slide 48
Slide 48 text
Dynamic Loading of Service Definitions
Sonntag, 6. Oktober 13
Slide 49
Slide 49 text
Use Semantic Configuration!
Sonntag, 6. Oktober 13
Slide 50
Slide 50 text
The Container as a Registry
Sonntag, 6. Oktober 13
Slide 51
Slide 51 text
The Container as a Registry
Sonntag, 6. Oktober 13
Slide 52
Slide 52 text
Instead: Proper Service Configuration
Sonntag, 6. Oktober 13
Slide 53
Slide 53 text
Binding to the Environment
Sonntag, 6. Oktober 13
Slide 54
Slide 54 text
Binding to the Environment
Sonntag, 6. Oktober 13
Slide 55
Slide 55 text
Instead: Use Your config files
Sonntag, 6. Oktober 13
Slide 56
Slide 56 text
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
Slide 57
Slide 57 text
Intermezzo
Random Tips
Sonntag, 6. Oktober 13
Slide 58
Slide 58 text
PHP
• Use 5.4/5.5, it‘s faster
• Use APC (or one of the alternatives)
Sonntag, 6. Oktober 13
Slide 59
Slide 59 text
Composer
• Use the --optimize-autoloader option
Sonntag, 6. Oktober 13
Slide 60
Slide 60 text
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
Slide 61
Slide 61 text
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
Slide 62
Slide 62 text
Translation
• Work with translation keys instead of
full text to avoid breaking translations
Sonntag, 6. Oktober 13
Slide 63
Slide 63 text
Searching
• Look for „Symfony2“
(without the space)
Sonntag, 6. Oktober 13
Slide 64
Slide 64 text
Read the Documentation
(and the Changelogs)
Sonntag, 6. Oktober 13
Slide 65
Slide 65 text
Chapter Four
Forms
Sonntag, 6. Oktober 13
Slide 66
Slide 66 text
Forms in Controllers
Sonntag, 6. Oktober 13
Slide 67
Slide 67 text
Forms in Controllers
Sonntag, 6. Oktober 13
Slide 68
Slide 68 text
• Couples Form setup to Controller
• No reusability
Sonntag, 6. Oktober 13
Slide 69
Slide 69 text
Better: Use Type Classes
Sonntag, 6. Oktober 13
Slide 70
Slide 70 text
Always set the data_class
Sonntag, 6. Oktober 13
Slide 71
Slide 71 text
Using Data in Constructors
Sonntag, 6. Oktober 13
Slide 72
Slide 72 text
Using Data in Constructors
Sonntag, 6. Oktober 13
Slide 73
Slide 73 text
Use Form Events for Related Data
Sonntag, 6. Oktober 13
Slide 74
Slide 74 text
Define Types as Services
Sonntag, 6. Oktober 13
Slide 75
Slide 75 text
Define Types as Services
Sonntag, 6. Oktober 13
Slide 76
Slide 76 text
Don‘t disable CSRF
Sonntag, 6. Oktober 13
Slide 77
Slide 77 text
Don‘t disable CSRF
Sonntag, 6. Oktober 13
Slide 78
Slide 78 text
Thanks! Questions?
Please give feedback:
http://goo.gl/yK97XK
Sonntag, 6. Oktober 13