PHP Tutorial Screencasts
Get Started, Move Fast
& Break things in
Symfony2
Slide 2
Slide 2 text
Who is this beach bum?
• The Symfony “Docs” guy
!
• KnpLabs US - Symfony consulting, training, Kumbaya
!
• Writer for KnpUniversity.com
screencasts
knplabs.com
github.com/weaverryan
PHP Tutorial Screencasts
Slide 3
Slide 3 text
Success!
Slide 4
Slide 4 text
Success?
Slide 5
Slide 5 text
How do we define it?
Slide 6
Slide 6 text
http://www.flickr.com/photos/martin_heigan/4544138976/
A
I delivered the Feature on time?
Slide 7
Slide 7 text
I came in under budget?
B
http://www.flickr.com/photos/68751915@N05/6355360253/
Slide 8
Slide 8 text
I delivered exactly what the
client asked for?
C
http://www.flickr.com/photos/adesigna/4090782772/
Slide 9
Slide 9 text
I delivered exactly what the
client asked for?
needed
C
http://www.flickr.com/photos/adesigna/4090782772/
Slide 10
Slide 10 text
D
The app has XX% test
coverage?
http://www.flickr.com/photos/sidelong/246816211/
Slide 11
Slide 11 text
E
My controllers average
6.35 lines?
http://www.flickr.com/photos/dharder9475/5381826270/
Slide 12
Slide 12 text
F
I use Symfony2 with
55 Bundles!
http://www.flickr.com/photos/sallypics/60614323/
Slide 13
Slide 13 text
Which one is it?
Slide 14
Slide 14 text
Define good…
- BDD/TDD is *good*, right?
- Skinny controllers are definitely
good… I think?
- 80%+ test coverage - is that good?
Slide 15
Slide 15 text
We measure success
entirely subjectively
Slide 16
Slide 16 text
thus…
Slide 17
Slide 17 text
Best practices & tools for
“success” are
subjectively decided
Slide 18
Slide 18 text
We can aim for better!
Slide 19
Slide 19 text
Measuring Success
X*Quality - (1-X)*Speed = Success Score
!
where X is 0-1, related to many factors
X = the Quality factor
Slide 20
Slide 20 text
1. Uncertainty
2. Mission Critical Features
3. Budget
X-Factors
Slide 21
Slide 21 text
1. How much uncertainty does the project
have?
2. Are you building what you *know* will be
successful, or is it more of an experiment?
3. High Uncertainty => Low X (less quality,
faster!)
Uncertainty
Slide 22
Slide 22 text
1. Are you protecting bank
data or patient records?
2. Or are you selling
Halloween costumes for
hamsters?
3. Mission Critical Features
=> High X (high quality,
slower!)
Mission Critical Features
http://www.etsy.com/listing/111520104/superman-hamster-costume-hamster-pet
Slide 23
Slide 23 text
1. No budget? No choice, fast!
2. No budget => low X (less quality,
faster!)
Budget
Slide 24
Slide 24 text
Let’s build *success*
Slide 25
Slide 25 text
Success:
!
Find what it means for
*your* project and hit it!
Slide 26
Slide 26 text
Writing a lot of tests
may mean lower success
Slide 27
Slide 27 text
Or it may
save yer arse
Slide 28
Slide 28 text
Or it may
save yer arse
@grmpyprogrammer
… from him
Slide 29
Slide 29 text
(Technical) Debt is not bad
Slide 30
Slide 30 text
try buying a house
without debt
Slide 31
Slide 31 text
Manage and leverage
technical debt
Slide 32
Slide 32 text
RAD in Symfony2
Slide 33
Slide 33 text
Symfony2 lends naturally
to high quality apps
(high X)
Slide 34
Slide 34 text
How can we go fast?
http://www.flickr.com/photos/curtisperry/141526923/
Slide 35
Slide 35 text
1. Shorten all the things
2. Routing
3. Querying for data
4. Rendering the
template
5. Fixtures
Speed up
6. Forms
7. Form theming
8. Service config
9. Security
10. Creating “special”
services
Slide 36
Slide 36 text
1
http://hyperboleandahalf.blogspot.com/2010/06/this-is-why-ill-never-be-adult.html
Shorten all the Things
Slide 37
Slide 37 text
A bundle called “App”
Slide 38
Slide 38 text
Brought to you by:
Slide 39
Slide 39 text
1. Allows you to have a bundle called simply
“App”
2. Shortens many things
3. KnpRadBundle has a philosophy (optional)
of only 1 bundle per project
KnpRadBundle “App”
Slide 40
Slide 40 text
a
2
Routing
http://www.flickr.com/photos/14617207@N00/4471342864/
Slide 41
Slide 41 text
A Routing File
Bundle name Controller
Slide 42
Slide 42 text
router:debug
Slide 43
Slide 43 text
1. Automatic CRUD routes
2. Ability to remove/add more
3. FOSRestBundle does this well for REST
controllers
4. SensioFrameworkExtraBundle allows for
annotation routing
KnpRadBundle Routing
Slide 44
Slide 44 text
No content
Slide 45
Slide 45 text
3
Querying for Data
Slide 46
Slide 46 text
Automatically Queried
Slide 47
Slide 47 text
!
1. Normally configured via a ParamConverter
annotation
2. Optional if:
A. You type-hint the argument
B. The routing wildcard is “id” or matches
the argument name
SensioFrameworkExtraBundle
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
A Routing File
teams is available as an argument
to any method in the controller
Slide 50
Slide 50 text
1. SensioFrameworkExtraBundle doesn’t
work to query for an array of objects
2. “teams” variable can be used across all
actions
KnpRadBundle Resources
Slide 51
Slide 51 text
4
Rendering the Template
Slide 52
Slide 52 text
Will Render App:Team:show.html.twig
App\Controller\TeamController::showAction
Slide 53
Slide 53 text
But wait, the template
doesn’t exist yet!!!
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
1. Finds the template and renders it
2. Creates it if it doesn’t exist
3. SensioFrameworkExtraBundle also does
this, but requires an annotation
KnpRadBundle
Creates and
binds the form
Creates and
binds the form
Shortcut methods
on KnpRadBundle
base Controller
Slide 68
Slide 68 text
1. Form is created automatically for you
based on the public getters/setters OR it
finds a {ClassName}Type and uses it
2. Form is automatically bound (if PUT/
POST)
KnpRadBundle Form Shortcuts
Slide 69
Slide 69 text
7
Form Theming!
Slide 70
Slide 70 text
1. Uses custom form themes for Twitter
Bootstrap
2. Install and it works
3. Comes with way too many other features,
but can easily be used for *just* the form
theming
MopaBootstraBundle
Slide 71
Slide 71 text
8
Service Configuration!
Slide 72
Slide 72 text
1. Normally done with a services.yml or
services.xml
2. KnpRadBundle automatically loads a
Resources/config/services.yml
Service Configuration
Slide 73
Slide 73 text
JMSDiExtraBundle allows
annotation configuration
Slide 74
Slide 74 text
No content
Slide 75
Slide 75 text
1. JmsDiExtraBundle saves you a little bit of
time, but I don’t love it
2. Seems to have cache issues
JmsDiExtraBundle?
Check for user roles
Custom object
permissions
(with voters)
Slide 80
Slide 80 text
SensioFrameworkExtraBundle
!
1. Flexible by using voters to hold complex
authorization logic
2. Easily add authorization and scan for it later
Slide 81
Slide 81 text
Special Services
10
Slide 82
Slide 82 text
Certain classes are auto-registered into
the service container by convention
KnpRadBundle
Slide 83
Slide 83 text
What? Dir
Doctrine Repo n/a
Twig Extension Twig/
Form Type Form/
Form Type Extension Form/Extension/
Validation Constraint Validator/Constraints/
Security Voter Security/
Auto-Service Registration
Slide 84
Slide 84 text
Using the auto-registered
repository service
Slide 85
Slide 85 text
Auto-Service Registration
1. Implement ContainerAwareInterface to be
passed the container
2. Offload logic to another service to avoid bad
practice of using the injected container
Slide 86
Slide 86 text
Part 3:
!
Iterating Quickly
Slide 87
Slide 87 text
Lets learn from our
experiments quickly
and adjust
Slide 88
Slide 88 text
1 Error Notification
1. Be ok with errors, but *know* about them
2. Get emailed on errors (or something
equivalent)*
3. Use NewRelic
* http://symfony.com/doc/current/cookbook/logging/monolog_email.html
Slide 89
Slide 89 text
2 Preventing or Fixing Errors
1. Code safely, but not too safely
2. Respond to a bug with effort that is
proportional to the pain it caused you
3. … so don’t freak out about every
complaint
Slide 90
Slide 90 text
3 Behat
Behat converts human-readable
descriptions of a feature into a
functional test
Slide 91
Slide 91 text
Behat team.feature file
Slide 92
Slide 92 text
No content
Slide 93
Slide 93 text
3 Behat
1. You already need to think about the
behavior of a feature
2. Fast to write, especially “smoke screen”
testing
3. Confidence that no core *behavior* is
broken on each depot
Slide 94
Slide 94 text
4 CI / Travis
1. Develop quickly without worrying about
tests
2. Always know if your app is in a
deployable state
Slide 95
Slide 95 text
5 PHPStorm
1. Community-developed “Symfony”
plugin
2. Autocompletion for service
names, route names, template
names, Twig functions, methods
on services
3. Click directly into templates,
classes behind a service
Slide 96
Slide 96 text
It’s basically ridiculous
Slide 97
Slide 97 text
1. RADBundle developer
2. Professional nice guy
3. Helped with this
presentation
6 Tips from Florian
@docteur_klein
Slide 98
Slide 98 text
6 Abstraction
@docteur_klein
Florian says:
Don’t over engineer with
abstraction layers
(YAGNI)
Slide 99
Slide 99 text
Maybe you need Silex
not Symfony
Slide 100
Slide 100 text
6 Small Classes
@docteur_klein
Florian says:
Create many small classes
(single responsibility
principle)
Slide 101
Slide 101 text
1. Library given you trouble? Based on the quality
factor, consider abandoning it
2. KnpRADBundle has *many* features, not all are
documented or are even a great idea. Use what
works, move on quickly.
7 Abandon problems
Slide 102
Slide 102 text
Final Act
!
Break (some) things
Slide 103
Slide 103 text
1. Define your success metric
A. How much uncertainty?
B. Mission critical features
C. Did we find any money?
RAD
Slide 104
Slide 104 text
2. Choose your RAD Features
• KnpRadBundle
• SensioFrameworkExtraBundle
• MopaBootstrapBundle
• Alice+Faker
• SonataAdminBundle
• APYDataGridBundle
• …
RAD
Slide 105
Slide 105 text
3. Iterate Quickly and with Confidence
• Error notification
• Behat
• CI / Travis
• PHPStorm + Symfony Plugin
• Avoid over-abstraction
• Abandon failing solutions (quickly, but
not too quickly)
RAD
Slide 106
Slide 106 text
Learn Faster
Slide 107
Slide 107 text
Make a more *successful* project
Slide 108
Slide 108 text
https://joind.in/10509
PHP Tutorial Screencasts
Ryan Weaver
@weaverryan