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
94
Symfony Internals
meandmymonkey
3
710
Divide and Conquer
meandmymonkey
1
590
Deptrac - Keep Your Architecture Clean
meandmymonkey
0
560
Introduction to Docker at PHPBenelux2015
meandmymonkey
3
760
Best Practices in Symfony2
meandmymonkey
0
300
Introduction to Docker at PHPNW2014
meandmymonkey
4
390
O(ops), Authentication!
meandmymonkey
4
750
Best Practices in Symfony2
meandmymonkey
15
1.7k
Other Decks in Programming
See All in Programming
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
1
690
Developer Joy == Developer Productivity (really!)
hollycummins
1
240
開発を加速する共有Swift Package実践
elmetal
PRO
0
420
Shinjuku.rb#95:心の技術書紹介
free_world21
1
110
Perl 5 OOP機構30年史 - Perl 5's OOP Mechanism over the past 30 years
moznion
0
410
Android開発以外のAndroid開発経験の活かしどころ
konifar
2
1.1k
Scala におけるコンパイラエラーとの付き合い方
chencmd
2
430
Google Sign-inの移行から始めるCredential Manager活用
clockvoid
0
420
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
200
GraphQL あるいは React における自律的なデータ取得について
quramy
11
3k
Regular Expressions, REXML, Automata Learning
makenowjust
0
220
なぜアジャイルがうまくいかないのか?
yum3
1
110
Featured
See All Featured
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
109
6.9k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
What’s in a name? Adding method to the madness
productmarketing
PRO
21
3k
Fantastic passwords and where to find them - at NoRuKo
philnash
48
2.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
190
16k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
354
29k
Why Our Code Smells
bkeepers
PRO
334
56k
Raft: Consensus for Rubyists
vanstee
136
6.5k
Scaling GitHub
holman
458
140k
Robots, Beer and Maslow
schacon
PRO
157
8.2k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
Mobile First: as difficult as doing things right
swwweet
221
8.8k
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