Best Practices in Symfony2
IPC Spring 2013 Berlin, June 4th
Andreas Hucks
Slide 2
Slide 2 text
@meandmymonkey
Andreas Hucks
Trainer & Consultant at
SensioLabs Deutschland
Slide 3
Slide 3 text
Best Practices
Slide 4
Slide 4 text
Bad
Practices
Slide 5
Slide 5 text
symfony Day 2009
Slide 6
Slide 6 text
Chapter One
A Clean Project
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
Slide 8
Slide 8 text
Care about your coding style
• Again - follow PSR-0, PSR-1, PSR-2
• Use PHPCSFixer
http://goo.gl/tEK4y
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
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 to be in your
global .gitignore
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
maybe this too
Committing parameters.yml is a
Three Kitten Offense
Slide 14
Slide 14 text
Remove Acme\*
Slide 15
Slide 15 text
Bundle Naming...
Vendor\AwesomeBundle
vs.
Vendor\Bundle\AwesomeBundle
Slide 16
Slide 16 text
Bundle Naming
Vendor\AwesomeBundle
vs.
Vendor\Bundle\AwesomeBundle
Slide 17
Slide 17 text
FAIL a.k.a. „because I can“
• MyCompleteAppBundle (ok for small projects)
• MyAppNeedingGlobalResourcesBundle
• MyBundleInsideAnotherBundleBundle
Slide 18
Slide 18 text
What should go into a Bundle
• Bundles should be self-contained
• Sets of Features
• Examples: Forum, AdminPanel...
• Configured in /app/config
Slide 19
Slide 19 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
Slide 20
Slide 20 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
meh.
Slide 21
Slide 21 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
special use cases
Slide 22
Slide 22 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
not for everything
Slide 23
Slide 23 text
Your config options
• YAML
• XML
• Annotations
• INI
• PHP
Routing, Bundle Config,
Parameters
Services
Validators, ORM/ODM
Slide 24
Slide 24 text
Nest your routing files
Slide 25
Slide 25 text
Chapter Two
Controllers
Slide 26
Slide 26 text
• Put Controllers on a Diet
• Decouple them
Slide 27
Slide 27 text
Try to lose the Boilerplate
Slide 28
Slide 28 text
@Template([name])
Slide 29
Slide 29 text
@Cache(...)
Slide 30
Slide 30 text
Inject the Request into Controllers
Slide 31
Slide 31 text
New in 2.3: handleRequest()
Slide 32
Slide 32 text
Events for add-on actions
Slide 33
Slide 33 text
Persistence Handlers
Slide 34
Slide 34 text
Persistence Handlers
Slide 35
Slide 35 text
Using the BaseController?
Wrap calls to the Container
Slide 36
Slide 36 text
Chapter Three
Dependency Injection
Slide 37
Slide 37 text
Injecting the Container
Slide 38
Slide 38 text
But... but Symfony is doing it!
Slide 39
Slide 39 text
...\TwigBundle\Extension\AssetsExtension
Slide 40
Slide 40 text
It‘s because of Scopes!
Slide 41
Slide 41 text
Solution: Synchronized Services (>= 2.3)
Slide 42
Slide 42 text
Alternative: Providing the Request (< 2.3)
Slide 43
Slide 43 text
Alternative: Providing the Request (< 2.3)
Slide 44
Slide 44 text
Service Organization
Slide 45
Slide 45 text
Split up Service Definitions
Slide 46
Slide 46 text
Dynamic Loading of Service Definitions
Slide 47
Slide 47 text
Use Semantic Configuration!
Slide 48
Slide 48 text
The Container as a Registry
Slide 49
Slide 49 text
Instead: Proper Service Configuration
Slide 50
Slide 50 text
Binding to the Environment
Slide 51
Slide 51 text
Instead: Use Your config files
Slide 52
Slide 52 text
Miscellaneous
• Use XML for Service Definitions
• Remember you can use Environment
Variables (Apache, Nginx, ...)
• Use %kernel.root_dir% as a reference
Slide 53
Slide 53 text
Intermezzo
Random Tips
Slide 54
Slide 54 text
PHP
• Use 5.4, it‘s faster
• Use APC (or one of the alternatives)
Slide 55
Slide 55 text
Composer
• Use the --optimize-autoloader option
Slide 56
Slide 56 text
Doctrine
• Activate Metadata Cache
• Activate Query Cache
• Use factory-service to register
Repositories & ObjectManagers as
Services
Slide 57
Slide 57 text
Security
• Make sure there are no leaks in the
security.yml access_control section!
• Better: Check Authorization in
Controller, possibly use
SecurityExtraBundle
Slide 58
Slide 58 text
Translation
• Work with translation keys instead of
full text to avoid breaking translations
Slide 59
Slide 59 text
Searching
• Look for „Symfony2“
(without the space)
Slide 60
Slide 60 text
Read the Documentation
(and the Changelogs)
Slide 61
Slide 61 text
Chapter Four
Forms
Slide 62
Slide 62 text
Forms in Controllers
Slide 63
Slide 63 text
• Couples Form setup to Controller
• No reusability
Slide 64
Slide 64 text
Better: Use Type Classes
Slide 65
Slide 65 text
Always set the data_class
Slide 66
Slide 66 text
Using Data in Constructors
Slide 67
Slide 67 text
Use Form Events for Related Data
Slide 68
Slide 68 text
Define Types as Services
Slide 69
Slide 69 text
Define Types as Services
Slide 70
Slide 70 text
Don‘t disable CSRF
Slide 71
Slide 71 text
Thanks! Questions?
Please give feedback:
http://goo.gl/kwEqY