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.6k
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
160
Symfony Internals
meandmymonkey
3
870
Divide and Conquer
meandmymonkey
1
670
Deptrac - Keep Your Architecture Clean
meandmymonkey
0
720
Introduction to Docker at PHPBenelux2015
meandmymonkey
3
850
Best Practices in Symfony2
meandmymonkey
0
450
Introduction to Docker at PHPNW2014
meandmymonkey
4
410
O(ops), Authentication!
meandmymonkey
4
920
Best Practices in Symfony2
meandmymonkey
15
1.8k
Other Decks in Programming
See All in Programming
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
360
Writing Better Go: Lessons from 10 Code Reviews
konradreiche
3
6.8k
社会人になっても趣味開発を続けたい! / traPavilion
mazrean
1
100
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
190
CSC509 Lecture 07
javiergs
PRO
0
240
『毎日の移動』を支えるGoバックエンド内製開発
yutautsugi
2
290
テーブル定義書の構造化抽出して、生成AIでDWH分析を試してみた / devio2025tokyo
kasacchiful
0
300
AIと人間の共創開発!OSSで試行錯誤した開発スタイル
mae616
2
810
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
1.2k
Developer Joy - The New Paradigm
hollycummins
1
370
Leading Effective Engineering Teams in the AI Era
addyosmani
7
630
What's new in Spring Modulith?
olivergierke
1
170
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Thoughts on Productivity
jonyablonski
70
4.9k
How to Ace a Technical Interview
jacobian
280
24k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
The World Runs on Bad Software
bkeepers
PRO
72
11k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.5k
Site-Speed That Sticks
csswizardry
13
930
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
22k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Building Adaptive Systems
keathley
44
2.8k
Building an army of robots
kneath
305
46k
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