Professional Documents
Culture Documents
30symfonybestpractices 090904074841 Phpapp01
30symfonybestpractices 090904074841 Phpapp01
30 Symfony Best
Practices
SymfonyDay ’09, Cologne, Germany
September 4th, 2009
Nicolas Perriault
30 Symfony Best Practices
Best Practices?
A Best practice is a technique, method, process, activity, incentive or
reward that is believed to be more effective at delivering a particular
outcome than any other technique, method, process, etc.The idea is
that with proper processes, checks, and testing, a desired outcome
can be delivered with fewer problems and unforeseen
complications. Best practices can also be defined as the most
efficient (least amount of effort) and effective (best results) way of
accomplishing a task, based on repeatable procedures that have
proven themselves over time for large numbers of people.
-- Wikipedia, august 2009
Nicolas Perriault 2
30 Symfony Best Practices
Best Practices?
Nicolas Perriault 3
30 Symfony Best Practices
Best Practices?
• Moreover, best practices in software
development and symfony are all about:
Nicolas Perriault 4
30 Symfony Best Practices
Disclaimer
• These best practices are not sorted by
importance, nor criticality
Let’s begin...
Nicolas Perriault
30 Symfony Best Practices
#0
Nicolas Perriault 7
30 Symfony Best Practices
#0
Nicolas Perriault 7
30 Symfony Best Practices
#1
• Manage the View within the View
Nicolas Perriault 8
30 Symfony Best Practices
#1
For example, handling <title> tag and assets:
Nicolas Perriault 9
30 Symfony Best Practices
#1
Nicolas Perriault 10
30 Symfony Best Practices
#2
Nicolas Perriault 11
30 Symfony Best Practices
GOOD
Nicolas Perriault 12
30 Symfony Best Practices
#3
• Always call a redirect after posting data
• for security purpose
• for ergonomy
• to avoid duplicates in database
• don’t forget to add a flash message to the
end user after every transaction made
Nicolas Perriault 13
30 Symfony Best Practices
BAD
Nicolas Perriault 14
30 Symfony Best Practices
GOOD
Nicolas Perriault 15
30 Symfony Best Practices
#4
• No Propel Criteria or Doctrine_Query
instances, SQL queries and any ORM/RDBMS
specific calls should ever be found neither in
the templates nor in the actions.
• The more you couple your model to your
controllers and views, the more it’ll be hard
to change your persistence backend or
strategy.
Nicolas Perriault 16
30 Symfony Best Practices
BAD
Doctrine_Collection
Nicolas Perriault 17
30 Symfony Best Practices
BETTER
Array
Nicolas Perriault 18
30 Symfony Best Practices
GOOD
Array (ideally)
Nicolas Perriault 19
30 Symfony Best Practices
#5
• Symfony core files should never be
modified to add or change the features they
provide, but replaced through the
autoloading mechanism, or way better:
extended.
• Tweaking the factories.yml file and
catching native symfony events can help a lot
achieving this goal.
Nicolas Perriault 20
30 Symfony Best Practices
BAD
Unused
Nicolas Perriault 21
30 Symfony Best Practices
GOOD
Nicolas Perriault 22
30 Symfony Best Practices
#6
Nicolas Perriault 23
30 Symfony Best Practices
BAD
Nicolas Perriault 24
30 Symfony Best Practices
GOOD
Nicolas Perriault 25
30 Symfony Best Practices
#7
• Verify cache settings, especially that the
cache is actually enabled in prod
environment. Don’t laugh, it happened quite
a lot.
• If you use cache, create a staging
environment to test the caching strategy if it
doesn't exist.
• Better: avoid using cache. No kidding.
Nicolas Perriault 26
30 Symfony Best Practices
GOOD
Nicolas Perriault 27
30 Symfony Best Practices
#8
• Use routes instead of raw module/action
couple in url_for(), link_to() and
redirect() calls.
Nicolas Perriault 28
30 Symfony Best Practices
#9
• All code comments, phpdoc, INSTALL,
CHANGELOG and README files, variables,
functions, classes and methods names and
more generally the developer
documentation, should be written in
English.
Nicolas Perriault 29
30 Symfony Best Practices
BAD
Nicolas Perriault 30
30 Symfony Best Practices
GOOD
Nicolas Perriault 31
30 Symfony Best Practices
#10
Nicolas Perriault 32
30 Symfony Best Practices
BAD
Nicolas Perriault 33
30 Symfony Best Practices
GOOD
Nicolas Perriault 34
30 Symfony Best Practices
#11
Nicolas Perriault 35
30 Symfony Best Practices
BAD
Nicolas Perriault 36
30 Symfony Best Practices
BAD
Nicolas Perriault 37
30 Symfony Best Practices
#12
• Ideally:
• A typical controller method should never
exceed ~30 lines.
• A typical controller class should never
have more than ~15 action methods.
Nicolas Perriault 38
x
30 Symfony Best Practices
BAD
Nicolas Perriault ~2000 lines on this actions class (the slidescreen size was to short to list them all). Unmaintenable. 39
30 Symfony Best Practices
#13
Nicolas Perriault 40
30 Symfony Best Practices
BAD
Nicolas Perriault 41
30 Symfony Best Practices
GOOD
Nicolas Perriault 42
30 Symfony Best Practices
#14
Nicolas Perriault 43
30 Symfony Best Practices
BAD
Nicolas Perriault 44
30 Symfony Best Practices
GOOD
Nicolas Perriault 45
30 Symfony Best Practices
#15
Nicolas Perriault 46
30 Symfony Best Practices
BAD
Nicolas Perriault 47
30 Symfony Best Practices
GOOD
Note: of course, a setRecentlySeenProducts() method could have been implemented in the myUser class.
Nicolas Perriault 48
30 Symfony Best Practices
#16
Nicolas Perriault 49
30 Symfony Best Practices
BAD
Nicolas Perriault 50
30 Symfony Best Practices
GOOD
Nicolas Perriault 51
30 Symfony Best Practices
#17
• There should never be any direct use of
sfContext inside the Model layer (eg.
sfContext::getInstance())
Nicolas Perriault 52
30 Symfony Best Practices
BAD
Nicolas Perriault 53
30 Symfony Best Practices
BAD
Nicolas Perriault 54
30 Symfony Best Practices
GOOD
Nicolas Perriault 55
30 Symfony Best Practices
GOOD
Nicolas Perriault 56
30 Symfony Best Practices
#18
• Avoid creating a big generic utility class
with many static methods
• It's like reinventing procedural
programming or using functions
• Write specialized classes
Nicolas Perriault 57
30 Symfony Best Practices
#19
Nicolas Perriault 58
30 Symfony Best Practices
GOOD
factories.yml
Nicolas Perriault 59
30 Symfony Best Practices
#20
• If source code is managed through a SCM
tool, versioned files should NEVER contain
passwords, local paths, etc.
Nicolas Perriault 60
30 Symfony Best Practices
BAD
Nicolas Perriault 61
30 Symfony Best Practices
GOOD
Nicolas Perriault 62
30 Symfony Best Practices
#21
• Write unit and functional tests:
• Unit test your business logic (please
don’t test symfony or Doctrine again, it’s
already done)
Nicolas Perriault 63
30 Symfony Best Practices
#21
Hint: you can write your own functional browser/
tester to avoid duplicate test code
Nicolas Perriault 64
30 Symfony Best Practices
#22
• Never use absolute paths in code, or at
least put them in YAML configuration files.
Nicolas Perriault 65
30 Symfony Best Practices
BAD
Nicolas Perriault 66
30 Symfony Best Practices
GOOD
Nicolas Perriault 67
30 Symfony Best Practices
#23
• Data objects should always be represented
by a slug in urls, not a primary key
Nicolas Perriault 68
30 Symfony Best Practices
#24
• Every application or project specific
configuration variable should be
stored in its app.yml file
Nicolas Perriault 69
30 Symfony Best Practices
BAD
Nicolas Perriault 70
30 Symfony Best Practices
GOOD
Default value
Nicolas Perriault 71
30 Symfony Best Practices
#25
• Avoid versioning any generated Base* class
file, especially if you want to write
reusable plugins or redistribute your
work.
• Maybe people will want to extend or add
behaviors to your model classes.
Nicolas Perriault 72
30 Symfony Best Practices
#26
• Use symfony tasks system for CLI batch
scripts
• Tasks have access to all the symfony
facilities
• Tasks are designed for the CLI
• Text output
• Pretty printing
Nicolas Perriault 73
30 Symfony Best Practices
#26
Nicolas Perriault 74
30 Symfony Best Practices
#27
• Alter request and response
programmatically by using filters.
Nicolas Perriault 75
30 Symfony Best Practices
GOOD
Nicolas Perriault 76
30 Symfony Best Practices
GOOD
Nicolas Perriault 77
30 Symfony Best Practices
#28
• Always write a README, an INSTALL and a
CHANGELOG file at the root of the project
(or the plugin). If open sourced, a LICENSE
file is appreciated too.
• And please don’t release plugins under the
terms of the GPL license.
Symfony Plugin != Operating System
Nicolas Perriault 78
30 Symfony Best Practices
#29
Nicolas Perriault 79
30 Symfony Best Practices
#30
• Study other frameworks code!
• They might have good, even better ideas
to solve the problem you have
• Even frameworks written in other
languages than PHP (Django, Rails,
Spring...)
Nicolas Perriault 80
30 Symfony Best Practices
Questions?
Nicolas Perriault
nperriault@gmail.com
+33 660 920 867
Nicolas Perriault