Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Best Practices in Symfony2
Search
Andreas Hucks
June 04, 2013
Programming
4.7k
28
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Best Practices in Symfony2
Given at International PHP Conference 2013
Andreas Hucks
June 04, 2013
Transcript
71 slides
Best Practices in Symfony2 IPC Spring 2013 Berlin, June 4th
Andreas Hucks
@meandmymonkey Andreas Hucks Trainer & Consultant at SensioLabs Deutschland
Best Practices
Bad Practices
symfony Day 2009
Chapter One A Clean Project
Naming Things in Symphony • Follow PSR-0, PSR-1, PSR-2 •
Find a common scheme for your team • Be explicit • Be consistent
Care about your coding style • Again - follow PSR-0,
PSR-1, PSR-2 • Use PHPCSFixer http://goo.gl/tEK4y
.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
.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
.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
.gitignore /web/bundles/ /app/bootstrap.php.cache /app/cache/* /app/config/parameters.yml /app/logs/* /build/ /vendor/ /bin/ /composer.phar
/data/uploads .idea this.
Committing parameters.yml is a Three Kitten Offense
Remove Acme\*
Bundle Naming... Vendor\AwesomeBundle vs. Vendor\Bundle\AwesomeBundle
Bundle Naming Vendor\AwesomeBundle vs. Vendor\Bundle\AwesomeBundle
FAIL a.k.a. „because I can“ • MyCompleteAppBundle (ok for small
projects) • MyAppNeedingGlobalResourcesBundle • MyBundleInsideAnotherBundleBundle
What should go into a Bundle • Bundles should be
self-contained • Sets of Features • Examples: Forum, AdminPanel... • Configured in /app/config
Your config options • YAML • XML • Annotations •
INI • PHP
Your config options • YAML • XML • Annotations •
INI • PHP meh.
Your config options • YAML • XML • Annotations •
INI • PHP special use cases
Your config options • YAML • XML • Annotations •
INI • PHP not for everything
Your config options • YAML • XML • Annotations •
INI • PHP Routing, Bundle Config, Parameters Services Validators, ORM/ODM
Nest your routing files
Chapter Two Controllers
• Put Controllers on a Diet • Decouple them
Try to lose the Boilerplate
@Template([name])
@Cache(...)
Inject the Request into Controllers
New in 2.3: handleRequest()
Events for add-on actions
Persistence Handlers
Persistence Handlers
Using the BaseController? Wrap calls to the Container
Chapter Three Dependency Injection
Injecting the Container
But... but Symfony is doing it!
...\TwigBundle\Extension\AssetsExtension
It‘s because of Scopes!
Solution: Synchronized Services (>= 2.3)
Alternative: Providing the Request (< 2.3)
Alternative: Providing the Request (< 2.3)
Service Organization
Split up Service Definitions
Dynamic Loading of Service Definitions
Use Semantic Configuration!
The Container as a Registry
Instead: Proper Service Configuration
Binding to the Environment
Instead: Use Your config files
Miscellaneous • Use XML for Service Definitions • Remember you
can use Environment Variables (Apache, Nginx, ...) • Use %kernel.root_dir% as a reference
Intermezzo Random Tips
PHP • Use 5.4, it‘s faster • Use APC (or
one of the alternatives)
Composer • Use the --optimize-autoloader option
Doctrine • Activate Metadata Cache • Activate Query Cache •
Use factory-service to register Repositories & ObjectManagers as Services
Security • Make sure there are no leaks in the
security.yml access_control section! • Better: Check Authorization in Controller, possibly use SecurityExtraBundle
Translation • Work with translation keys instead of full text
to avoid breaking translations
Searching • Look for „Symfony2“ (without the space)
Read the Documentation (and the Changelogs)
Chapter Four Forms
Forms in Controllers
• Couples Form setup to Controller • No reusability
Better: Use Type Classes
Always set the data_class
Using Data in Constructors
Use Form Events for Related Data
Define Types as Services
Define Types as Services
Don‘t disable CSRF
Thanks! Questions? Please give feedback: http://goo.gl/kwEqY
More Decks by Andreas Hucks
See All by Andreas Hucks
Divide and Conquer (LonghornPHP 2019)
meandmymonkey
0
240
Symfony Internals
meandmymonkey
3
980
Divide and Conquer
meandmymonkey
1
770
Deptrac - Keep Your Architecture Clean
meandmymonkey
0
890
Introduction to Docker at PHPBenelux2015
meandmymonkey
3
970
Best Practices in Symfony2
meandmymonkey
0
570
Introduction to Docker at PHPNW2014
meandmymonkey
4
450
O(ops), Authentication!
meandmymonkey
4
1.1k
Best Practices in Symfony2
meandmymonkey
15
1.9k
Other Decks in Programming
See All in Programming
Kiro Crewによる開発体験ーKiro CrewでStrands Decider 2B に関する技術検証をやってみた
satoshi256kbyte
1
230
imagewebp()の向こう側: WebPはなぜ JPEG・PNGより軽いのか
yuzneri
0
160
AIコーディング時代のKotest ユニットテストから APIレベルの仕様固定化テストへ
ooga04
0
350
初学者向け 分散トレーシングとDatadog
mtaka1222
0
110
眠らない開発チームの作り方
biwakonbu
0
170
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
190
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
210
joyful-journey-spring-boot.pdf
hollycummins
0
130
AIエージェントの「テストは通るのに本番で壊れる」を減らす
shibuiwilliam
3
130
エラーチャンネルのボールを拾い続けて鍛えるエンジニアの地力 / phpcon-niigata2026
meihei3
1
530
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
210
手の動かし方がわからなかった私が個人開発者になるまで
usagi0920_pochi
0
350
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
610
Digital Ethics as a Driver of Design Innovation
axbom
PRO
2
570
Leo the Paperboy
mayatellez
11
2.5k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
500
Web Performance Is a Journey [PerfNow 2026]
tammyeverts
3
180
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
It's Worth the Effort
3n
188
30k
Designing Experiences People Love
moore
143
24k
Darren the Foodie - Storyboard
khoart
PRO
5
4.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k