Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Best Practices in Symfony2
Search
Andreas Hucks
June 04, 2013
Programming
28
4.5k
Best Practices in Symfony2
Given at International PHP Conference 2013
Andreas Hucks
June 04, 2013
Tweet
Share
More Decks by Andreas Hucks
See All by Andreas Hucks
Divide and Conquer (LonghornPHP 2019)
meandmymonkey
0
100
Symfony Internals
meandmymonkey
3
720
Divide and Conquer
meandmymonkey
1
600
Deptrac - Keep Your Architecture Clean
meandmymonkey
0
590
Introduction to Docker at PHPBenelux2015
meandmymonkey
3
780
Best Practices in Symfony2
meandmymonkey
0
320
Introduction to Docker at PHPNW2014
meandmymonkey
4
390
O(ops), Authentication!
meandmymonkey
4
770
Best Practices in Symfony2
meandmymonkey
15
1.7k
Other Decks in Programming
See All in Programming
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
120
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
240
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.3k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Jakarta EE meets AI
ivargrimstad
0
240
React への依存を最小にするフロントエンド設計
takonda
3
530
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
Jakarta EE meets AI
ivargrimstad
0
620
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Featured
See All Featured
Unsuck your backbone
ammeep
668
57k
Designing the Hi-DPI Web
ddemaree
280
34k
Ruby is Unlike a Banana
tanoku
97
11k
A designer walks into a library…
pauljervisheath
204
24k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Faster Mobile Websites
deanohume
305
30k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
The Cult of Friendly URLs
andyhume
78
6k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Become a Pro
speakerdeck
PRO
25
5k
Building Applications with DynamoDB
mza
90
6.1k
Transcript
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