SlideShare a Scribd company logo
30 Symfony Best Practices




                     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?


          Making. Stuff. Efficient.



 Nicolas Perriault                     3
30 Symfony Best Practices



                     Best Practices?
        •    Moreover, best practices in software
             development and symfony are all about:

            •   improving communication between
                developers through standardization

            •   enhancing security, maintainability, portability
                and interoperability by increasing code
                quality


 Nicolas Perriault                                                 4
30 Symfony Best Practices



                       Disclaimer
        •    These best practices are not sorted by
             importance, nor criticality

        •    All best practices listed here are debatable, at
             least because they’re based on my own
             experience

        •    There are always exceptions

        •    Some best practices are not symfony-specific ;
             “Zake Igniter” developers, you can take some notes
             too.
 Nicolas Perriault                                                5
30 Symfony Best Practices




                     Let’s begin...


 Nicolas Perriault
30 Symfony Best Practices



                             #0

        •    Always write “symfony” starting with a
             small cap.




 Nicolas Perriault                                    7
30 Symfony Best Practices



                                   #0

        •    Always write “symfony” starting with a
             small cap.
        •    Or maybe not (mostly depends on the result of rand(0,   1))




 Nicolas Perriault                                                         7
30 Symfony Best Practices



                                 #1
        •    Manage the View within the View

            •   View is intended to be handled in templates

            •   Try to avoid the use of the view.yml file

            •   Try to avoid handling the view from the
                controller (eeeek) or the model (you’re
                fired)

            •   Slots can help (a lot)

 Nicolas Perriault                                            8
30 Symfony Best Practices



                                                               #1
  For example, handling <title> tag and assets:




                Note: here the value of $defaultTitle could (should) be handled
                                in an app.yml configuration file




 Nicolas Perriault                                                                9
30 Symfony Best Practices



                               #1

        • Remember this: if you’re a graphic designer/
             integrator, you don’t want to deal with YAML or
             complex PHP code to manage the presentation




 Nicolas Perriault                                             10
30 Symfony Best Practices



                             #2

        • Always enable output escaping and CSRF
             protection
            • starting with symfony 1.3, they’re enabled
                by default



 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

        • The templates must contain PHP
             alternative templating syntax, for
             readability and ease of use.




 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.

        • Also, default routes should be deactivated
             by default (at least in apps which don’t run
             admin-generator 1.0 modules)


 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

        • Catch and log exception messages, display
             human readable error messages to the end
             user




 Nicolas Perriault                                      32
30 Symfony Best Practices



                            BAD




 Nicolas Perriault                33
30 Symfony Best Practices



                      GOOD




 Nicolas Perriault           34
30 Symfony Best Practices



                            #11


        • Only deploy production front controllers in
             production




 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

        • Always apply consistent coding
             standards.
        • Always apply Symfony’s coding standards.
             http://trac.symfony-project.org/wiki/HowToContributeToSymfony#CodingStandards




 Nicolas Perriault                                                                           40
30 Symfony Best Practices



                            BAD




 Nicolas Perriault                41
30 Symfony Best Practices



                      GOOD




 Nicolas Perriault           42
30 Symfony Best Practices



                            #14

        • Session persistence logic should
             reside in sfUser derived class methods, and
             only them.




 Nicolas Perriault                                         43
30 Symfony Best Practices



                            BAD




 Nicolas Perriault                44
30 Symfony Best Practices



                      GOOD




 Nicolas Perriault           45
30 Symfony Best Practices



                            #15


        • Never serialize objects in the session


 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


        • Always create customized 404 error and
             500 error pages




 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())

            •   Because the sfContext instance you get can
                differ a lot regarding the used env (cli, test,
                dev, prod...)

            •   It would make your code quite untestable

        •    Yes, it’s hard.

 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

        • Use sfLogger for debugging instead of
             echoing variable or debug messages
        •    FirePHP(1) or FireSymfony(2) custom loggers
             can be more than helpful to debug your app
             (webservices for example)
            (1) http://firephp.org/
            (2) http://firesymfony.org/



 Nicolas Perriault                                         58
30 Symfony Best Practices



                              GOOD
                                                               factories.yml




     The FireSymfony Firebug extension for Firefox in action

 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.

            •   Files containing them should be added to the
                ignore directive

            •   Typical example: the databases.yml file

            •   Distribute platform-dependent configuration
                files

 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 symfony business logicit’s
             don’t
                   test your
                               or Doctrine again,
                                                  (please

                     already done)

               • Functionally test the user interface
                     when it’s important (eg.
                     404/403/401 HTTP codes, security and user
                     authentication, transactions and forms, etc.)

 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.

        •    Make your symfony project portable: use
             dirname(__FILE__) to point at the symfony
             libs in the project configuration class.
        •    You can put symfony lib in a lib/vendor dir.


 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

            • For security: numeric PKs are easily
                guessable, whereas slugs are not
            • For URL usability and SEO:
                /article/symfony-rulez.html   is sexier
                than /article/123.html

 Nicolas Perriault                                        68
30 Symfony Best Practices



                             #24
        • Every application or project specific
             configuration variable should be
             stored in its app.yml file

        • You can share project-wide (cross-
             applications) settings by putting an app.yml
             file within the root config/ folder of the
             project

 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.

        • You can use the request.filter_parameters
             and response.filter_content events too.

        • Filters are easy to setup and to debug,
             whereas events are more clean and
             powerful. Matter of taste.

 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

        • Share features and modules across
             applications by writing plugins

        • If it’s really cool, release it publicly

 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


          prendreuncafe.com | symfonians.net | symfony-project.org



 Nicolas Perriault

More Related Content

What's hot

Symfony Best Practices
Symfony Best PracticesSymfony Best Practices
Symfony Best Practices
Baptiste Donaux
 
Ansible Automation to Rule Them All
Ansible Automation to Rule Them AllAnsible Automation to Rule Them All
Ansible Automation to Rule Them All
Tim Fairweather
 
Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...
Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...
Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...
Edureka!
 
Introduction to Vagrant
Introduction to VagrantIntroduction to Vagrant
Introduction to Vagrant
Marcelo Pinheiro
 
Terraform
TerraformTerraform
Terraform
An Nguyen
 
給你一個使用 Laravel 的理由
給你一個使用 Laravel 的理由給你一個使用 Laravel 的理由
給你一個使用 Laravel 的理由
Shengyou Fan
 
Alphorm.com Formation Kubernetes : Installation et Configuration
Alphorm.com Formation Kubernetes : Installation et ConfigurationAlphorm.com Formation Kubernetes : Installation et Configuration
Alphorm.com Formation Kubernetes : Installation et Configuration
Alphorm
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
Rayed Alrashed
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
George Shuklin
 
Ansible, best practices
Ansible, best practicesAnsible, best practices
Ansible, best practices
Bas Meijer
 
Concourse and Database
Concourse and DatabaseConcourse and Database
Concourse and Database
Gwenn Etourneau
 
Terraform
TerraformTerraform
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
Simplilearn
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to Chef
Knoldus Inc.
 
Terraform modules restructured
Terraform modules restructuredTerraform modules restructured
Terraform modules restructured
Ami Mahloof
 
使用 Controller
使用 Controller使用 Controller
使用 Controller
Shengyou Fan
 
Configuration Management in Ansible
Configuration Management in Ansible Configuration Management in Ansible
Configuration Management in Ansible
Bangladesh Network Operators Group
 
Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...
Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...
Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...
Edureka!
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform Training
Yevgeniy Brikman
 
Configuration dns sous linux
Configuration  dns sous linuxConfiguration  dns sous linux
Configuration dns sous linux
Balamine Gassama
 

What's hot (20)

Symfony Best Practices
Symfony Best PracticesSymfony Best Practices
Symfony Best Practices
 
Ansible Automation to Rule Them All
Ansible Automation to Rule Them AllAnsible Automation to Rule Them All
Ansible Automation to Rule Them All
 
Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...
Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...
Dockerfile Tutorial with Example | Creating your First Dockerfile | Docker Tr...
 
Introduction to Vagrant
Introduction to VagrantIntroduction to Vagrant
Introduction to Vagrant
 
Terraform
TerraformTerraform
Terraform
 
給你一個使用 Laravel 的理由
給你一個使用 Laravel 的理由給你一個使用 Laravel 的理由
給你一個使用 Laravel 的理由
 
Alphorm.com Formation Kubernetes : Installation et Configuration
Alphorm.com Formation Kubernetes : Installation et ConfigurationAlphorm.com Formation Kubernetes : Installation et Configuration
Alphorm.com Formation Kubernetes : Installation et Configuration
 
IT Automation with Ansible
IT Automation with AnsibleIT Automation with Ansible
IT Automation with Ansible
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
Ansible, best practices
Ansible, best practicesAnsible, best practices
Ansible, best practices
 
Concourse and Database
Concourse and DatabaseConcourse and Database
Concourse and Database
 
Terraform
TerraformTerraform
Terraform
 
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
What Is Ansible? | How Ansible Works? | Ansible Tutorial For Beginners | DevO...
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to Chef
 
Terraform modules restructured
Terraform modules restructuredTerraform modules restructured
Terraform modules restructured
 
使用 Controller
使用 Controller使用 Controller
使用 Controller
 
Configuration Management in Ansible
Configuration Management in Ansible Configuration Management in Ansible
Configuration Management in Ansible
 
Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...
Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...
Docker Commands With Examples | Docker Tutorial | DevOps Tutorial | Docker Tr...
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform Training
 
Configuration dns sous linux
Configuration  dns sous linuxConfiguration  dns sous linux
Configuration dns sous linux
 

Viewers also liked

New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
Javier Eguiluz
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
Javier Eguiluz
 
Symfony2: 30 astuces et bonnes pratiques
Symfony2: 30 astuces et bonnes pratiquesSymfony2: 30 astuces et bonnes pratiques
Symfony2: 30 astuces et bonnes pratiques
Noel GUILBERT
 
Dev traning 2016 symfony
Dev traning 2016   symfonyDev traning 2016   symfony
Dev traning 2016 symfony
Sacheen Dhanjie
 
Mastering Twig (DrupalCon Barcelona 2015)
Mastering Twig (DrupalCon Barcelona 2015)Mastering Twig (DrupalCon Barcelona 2015)
Mastering Twig (DrupalCon Barcelona 2015)
Javier Eguiluz
 
Design patterns avec Symfony
Design patterns avec SymfonyDesign patterns avec Symfony
Design patterns avec Symfony
Mohammed Rhamnia
 
Symfony Components
Symfony ComponentsSymfony Components
Symfony Components
Fabien Potencier
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
Haehnchen
 
Alphorm.com Support de la Formation Symfony 3 , les fondamentaux-ss
Alphorm.com Support de la Formation Symfony 3 , les fondamentaux-ssAlphorm.com Support de la Formation Symfony 3 , les fondamentaux-ss
Alphorm.com Support de la Formation Symfony 3 , les fondamentaux-ss
Alphorm
 
ゲームツクール第8回「Unity×Photon Cloud」
ゲームツクール第8回「Unity×Photon Cloud」ゲームツクール第8回「Unity×Photon Cloud」
ゲームツクール第8回「Unity×Photon Cloud」
ゲームツクール!
 
Symfony2 - garść porad
Symfony2 - garść poradSymfony2 - garść porad
Symfony2 - garść porad
Michał Kurzeja
 
symfony 1.1 goodness (Dutch PHP Conference 2008)
symfony 1.1 goodness (Dutch PHP Conference 2008)symfony 1.1 goodness (Dutch PHP Conference 2008)
symfony 1.1 goodness (Dutch PHP Conference 2008)
Fabien Potencier
 
Symfony bundle fo asynchronous job processing
Symfony bundle fo asynchronous job processingSymfony bundle fo asynchronous job processing
Symfony bundle fo asynchronous job processing
Wojciech Ciołko
 
Symfony Components 2.0 on PHP 5.3
Symfony Components 2.0 on PHP 5.3Symfony Components 2.0 on PHP 5.3
Symfony Components 2.0 on PHP 5.3
Fabien Potencier
 
Debugging and Profiling Symfony Apps
Debugging and Profiling Symfony AppsDebugging and Profiling Symfony Apps
Debugging and Profiling Symfony Apps
Alvaro Videla
 
symfony Live 2010 - Using Doctrine Migrations
symfony Live 2010 -  Using Doctrine Migrationssymfony Live 2010 -  Using Doctrine Migrations
symfony Live 2010 - Using Doctrine Migrations
D
 
Asynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketw
Asynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketwAsynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketw
Asynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketw
Luke Adamczewski
 
Przetwarzanie asynchroniczne w zastosowaniach webowych
Przetwarzanie asynchroniczne w zastosowaniach webowychPrzetwarzanie asynchroniczne w zastosowaniach webowych
Przetwarzanie asynchroniczne w zastosowaniach webowych
leafnode
 
Clever and innovative use of trends in HR
Clever and innovative use of trends in HRClever and innovative use of trends in HR
Clever and innovative use of trends in HR
Tom Haak
 
Building a documented RESTful API in just a few hours with Symfony
Building a documented RESTful API in just a few hours with SymfonyBuilding a documented RESTful API in just a few hours with Symfony
Building a documented RESTful API in just a few hours with Symfony
olrandir
 

Viewers also liked (20)

New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
 
Symfony tips and tricks
Symfony tips and tricksSymfony tips and tricks
Symfony tips and tricks
 
Symfony2: 30 astuces et bonnes pratiques
Symfony2: 30 astuces et bonnes pratiquesSymfony2: 30 astuces et bonnes pratiques
Symfony2: 30 astuces et bonnes pratiques
 
Dev traning 2016 symfony
Dev traning 2016   symfonyDev traning 2016   symfony
Dev traning 2016 symfony
 
Mastering Twig (DrupalCon Barcelona 2015)
Mastering Twig (DrupalCon Barcelona 2015)Mastering Twig (DrupalCon Barcelona 2015)
Mastering Twig (DrupalCon Barcelona 2015)
 
Design patterns avec Symfony
Design patterns avec SymfonyDesign patterns avec Symfony
Design patterns avec Symfony
 
Symfony Components
Symfony ComponentsSymfony Components
Symfony Components
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
 
Alphorm.com Support de la Formation Symfony 3 , les fondamentaux-ss
Alphorm.com Support de la Formation Symfony 3 , les fondamentaux-ssAlphorm.com Support de la Formation Symfony 3 , les fondamentaux-ss
Alphorm.com Support de la Formation Symfony 3 , les fondamentaux-ss
 
ゲームツクール第8回「Unity×Photon Cloud」
ゲームツクール第8回「Unity×Photon Cloud」ゲームツクール第8回「Unity×Photon Cloud」
ゲームツクール第8回「Unity×Photon Cloud」
 
Symfony2 - garść porad
Symfony2 - garść poradSymfony2 - garść porad
Symfony2 - garść porad
 
symfony 1.1 goodness (Dutch PHP Conference 2008)
symfony 1.1 goodness (Dutch PHP Conference 2008)symfony 1.1 goodness (Dutch PHP Conference 2008)
symfony 1.1 goodness (Dutch PHP Conference 2008)
 
Symfony bundle fo asynchronous job processing
Symfony bundle fo asynchronous job processingSymfony bundle fo asynchronous job processing
Symfony bundle fo asynchronous job processing
 
Symfony Components 2.0 on PHP 5.3
Symfony Components 2.0 on PHP 5.3Symfony Components 2.0 on PHP 5.3
Symfony Components 2.0 on PHP 5.3
 
Debugging and Profiling Symfony Apps
Debugging and Profiling Symfony AppsDebugging and Profiling Symfony Apps
Debugging and Profiling Symfony Apps
 
symfony Live 2010 - Using Doctrine Migrations
symfony Live 2010 -  Using Doctrine Migrationssymfony Live 2010 -  Using Doctrine Migrations
symfony Live 2010 - Using Doctrine Migrations
 
Asynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketw
Asynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketwAsynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketw
Asynchroniczny PHP i komunikacja czasu rzeczywistego z wykorzystaniem websocketw
 
Przetwarzanie asynchroniczne w zastosowaniach webowych
Przetwarzanie asynchroniczne w zastosowaniach webowychPrzetwarzanie asynchroniczne w zastosowaniach webowych
Przetwarzanie asynchroniczne w zastosowaniach webowych
 
Clever and innovative use of trends in HR
Clever and innovative use of trends in HRClever and innovative use of trends in HR
Clever and innovative use of trends in HR
 
Building a documented RESTful API in just a few hours with Symfony
Building a documented RESTful API in just a few hours with SymfonyBuilding a documented RESTful API in just a few hours with Symfony
Building a documented RESTful API in just a few hours with Symfony
 

Similar to 30 Symfony Best Practices

Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)
Stefan Koopmanschap
 
Myphp-busters: symfony framework
Myphp-busters: symfony frameworkMyphp-busters: symfony framework
Myphp-busters: symfony framework
Stefan Koopmanschap
 
Write Plugins for symfony (Symfony Camp 2007)
Write Plugins for symfony (Symfony Camp 2007)Write Plugins for symfony (Symfony Camp 2007)
Write Plugins for symfony (Symfony Camp 2007)
Fabien Potencier
 
Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)
Stefan Koopmanschap
 
symfony: Open-Source Enterprise Framework
symfony: Open-Source Enterprise Frameworksymfony: Open-Source Enterprise Framework
symfony: Open-Source Enterprise Framework
Fabien Potencier
 
Symfony workshop introductory slides
Symfony workshop introductory slidesSymfony workshop introductory slides
Symfony workshop introductory slides
Stefan Koopmanschap
 
Apple techmanuals verd
Apple techmanuals verdApple techmanuals verd
Apple techmanuals verd
Sherri Gunder
 

Similar to 30 Symfony Best Practices (7)

Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)
 
Myphp-busters: symfony framework
Myphp-busters: symfony frameworkMyphp-busters: symfony framework
Myphp-busters: symfony framework
 
Write Plugins for symfony (Symfony Camp 2007)
Write Plugins for symfony (Symfony Camp 2007)Write Plugins for symfony (Symfony Camp 2007)
Write Plugins for symfony (Symfony Camp 2007)
 
Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)
 
symfony: Open-Source Enterprise Framework
symfony: Open-Source Enterprise Frameworksymfony: Open-Source Enterprise Framework
symfony: Open-Source Enterprise Framework
 
Symfony workshop introductory slides
Symfony workshop introductory slidesSymfony workshop introductory slides
Symfony workshop introductory slides
 
Apple techmanuals verd
Apple techmanuals verdApple techmanuals verd
Apple techmanuals verd
 

Recently uploaded

WhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring AppsWhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring Apps
HackersList
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
BrainSell Technologies
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
ldtexsolbl
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
Priyanka Aash
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
maigasapphire
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
Google Developer Group - Harare
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
sunilverma7884
 
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes..."Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
Anant Gupta
 
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
aslasdfmkhan4750
 
CiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.pptCiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.ppt
moinahousna
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 
Opencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of MünsterOpencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of Münster
Matthias Neugebauer
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
SAI KAILASH R
 
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSECHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
kumarjarun2010
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
Zilliz
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
CEPTES Software Inc
 
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python CodebaseEuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
Jimmy Lai
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Torry Harris
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
HackersList
 
RPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptx
RPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptxRPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptx
RPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptx
SynapseIndia
 

Recently uploaded (20)

WhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring AppsWhatsApp Spy Online Trackers and Monitoring Apps
WhatsApp Spy Online Trackers and Monitoring Apps
 
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdfAcumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
Acumatica vs. Sage Intacct vs. NetSuite _ NOW CFO.pdf
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
 
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
(CISOPlatform Summit & SACON 2024) Keynote _ Power Digital Identities With AI...
 
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
Girls Call Churchgate 9910780858 Provide Best And Top Girl Service And No1 in...
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
 
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
Girls call Kolkata 👀 XXXXXXXXXXX 👀 Rs.9.5 K Cash Payment With Room Delivery
 
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes..."Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
"Mastering Graphic Design: Essential Tips and Tricks for Beginners and Profes...
 
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
High Profile Girls Call ServiCe Hyderabad 0000000000 Tanisha Best High Class ...
 
CiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.pptCiscoIconsLibrary cours de réseau VLAN.ppt
CiscoIconsLibrary cours de réseau VLAN.ppt
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 
Opencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of MünsterOpencast Summit 2024 — Opencast @ University of Münster
Opencast Summit 2024 — Opencast @ University of Münster
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
 
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSECHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
CHAPTER-8 COMPONENTS OF COMPUTER SYSTEM CLASS 9 CBSE
 
Using LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and MilvusUsing LLM Agents with Llama 3, LangGraph and Milvus
Using LLM Agents with Llama 3, LangGraph and Milvus
 
Salesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot WorkshopSalesforce AI & Einstein Copilot Workshop
Salesforce AI & Einstein Copilot Workshop
 
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python CodebaseEuroPython 2024 - Streamlining Testing in a Large Python Codebase
EuroPython 2024 - Streamlining Testing in a Large Python Codebase
 
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...Evolution of iPaaS - simplify IT workloads to provide a unified view of  data...
Evolution of iPaaS - simplify IT workloads to provide a unified view of data...
 
How Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdfHow Social Media Hackers Help You to See Your Wife's Message.pdf
How Social Media Hackers Help You to See Your Wife's Message.pdf
 
RPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptx
RPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptxRPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptx
RPA In Healthcare Benefits, Use Case, Trend And Challenges 2024.pptx
 

30 Symfony Best Practices

  • 1. 30 Symfony Best Practices 30 Symfony Best Practices SymfonyDay ’09, Cologne, Germany September 4th, 2009 Nicolas Perriault
  • 2. 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
  • 3. 30 Symfony Best Practices Best Practices? Making. Stuff. Efficient. Nicolas Perriault 3
  • 4. 30 Symfony Best Practices Best Practices? • Moreover, best practices in software development and symfony are all about: • improving communication between developers through standardization • enhancing security, maintainability, portability and interoperability by increasing code quality Nicolas Perriault 4
  • 5. 30 Symfony Best Practices Disclaimer • These best practices are not sorted by importance, nor criticality • All best practices listed here are debatable, at least because they’re based on my own experience • There are always exceptions • Some best practices are not symfony-specific ; “Zake Igniter” developers, you can take some notes too. Nicolas Perriault 5
  • 6. 30 Symfony Best Practices Let’s begin... Nicolas Perriault
  • 7. 30 Symfony Best Practices #0 • Always write “symfony” starting with a small cap. Nicolas Perriault 7
  • 8. 30 Symfony Best Practices #0 • Always write “symfony” starting with a small cap. • Or maybe not (mostly depends on the result of rand(0, 1)) Nicolas Perriault 7
  • 9. 30 Symfony Best Practices #1 • Manage the View within the View • View is intended to be handled in templates • Try to avoid the use of the view.yml file • Try to avoid handling the view from the controller (eeeek) or the model (you’re fired) • Slots can help (a lot) Nicolas Perriault 8
  • 10. 30 Symfony Best Practices #1 For example, handling <title> tag and assets: Note: here the value of $defaultTitle could (should) be handled in an app.yml configuration file Nicolas Perriault 9
  • 11. 30 Symfony Best Practices #1 • Remember this: if you’re a graphic designer/ integrator, you don’t want to deal with YAML or complex PHP code to manage the presentation Nicolas Perriault 10
  • 12. 30 Symfony Best Practices #2 • Always enable output escaping and CSRF protection • starting with symfony 1.3, they’re enabled by default Nicolas Perriault 11
  • 13. 30 Symfony Best Practices GOOD Nicolas Perriault 12
  • 14. 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
  • 15. 30 Symfony Best Practices BAD Nicolas Perriault 14
  • 16. 30 Symfony Best Practices GOOD Nicolas Perriault 15
  • 17. 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
  • 18. 30 Symfony Best Practices BAD Doctrine_Collection Nicolas Perriault 17
  • 19. 30 Symfony Best Practices BETTER Array Nicolas Perriault 18
  • 20. 30 Symfony Best Practices GOOD Array (ideally) Nicolas Perriault 19
  • 21. 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
  • 22. 30 Symfony Best Practices BAD Unused Nicolas Perriault 21
  • 23. 30 Symfony Best Practices GOOD Nicolas Perriault 22
  • 24. 30 Symfony Best Practices #6 • The templates must contain PHP alternative templating syntax, for readability and ease of use. Nicolas Perriault 23
  • 25. 30 Symfony Best Practices BAD Nicolas Perriault 24
  • 26. 30 Symfony Best Practices GOOD Nicolas Perriault 25
  • 27. 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
  • 28. 30 Symfony Best Practices GOOD Nicolas Perriault 27
  • 29. 30 Symfony Best Practices #8 • Use routes instead of raw module/action couple in url_for(), link_to() and redirect() calls. • Also, default routes should be deactivated by default (at least in apps which don’t run admin-generator 1.0 modules) Nicolas Perriault 28
  • 30. 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
  • 31. 30 Symfony Best Practices BAD Nicolas Perriault 30
  • 32. 30 Symfony Best Practices GOOD Nicolas Perriault 31
  • 33. 30 Symfony Best Practices #10 • Catch and log exception messages, display human readable error messages to the end user Nicolas Perriault 32
  • 34. 30 Symfony Best Practices BAD Nicolas Perriault 33
  • 35. 30 Symfony Best Practices GOOD Nicolas Perriault 34
  • 36. 30 Symfony Best Practices #11 • Only deploy production front controllers in production Nicolas Perriault 35
  • 37. 30 Symfony Best Practices BAD Nicolas Perriault 36
  • 38. 30 Symfony Best Practices BAD Nicolas Perriault 37
  • 39. 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
  • 40. 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
  • 41. 30 Symfony Best Practices #13 • Always apply consistent coding standards. • Always apply Symfony’s coding standards. http://trac.symfony-project.org/wiki/HowToContributeToSymfony#CodingStandards Nicolas Perriault 40
  • 42. 30 Symfony Best Practices BAD Nicolas Perriault 41
  • 43. 30 Symfony Best Practices GOOD Nicolas Perriault 42
  • 44. 30 Symfony Best Practices #14 • Session persistence logic should reside in sfUser derived class methods, and only them. Nicolas Perriault 43
  • 45. 30 Symfony Best Practices BAD Nicolas Perriault 44
  • 46. 30 Symfony Best Practices GOOD Nicolas Perriault 45
  • 47. 30 Symfony Best Practices #15 • Never serialize objects in the session Nicolas Perriault 46
  • 48. 30 Symfony Best Practices BAD Nicolas Perriault 47
  • 49. 30 Symfony Best Practices GOOD Note: of course, a setRecentlySeenProducts() method could have been implemented in the myUser class. Nicolas Perriault 48
  • 50. 30 Symfony Best Practices #16 • Always create customized 404 error and 500 error pages Nicolas Perriault 49
  • 51. 30 Symfony Best Practices BAD Nicolas Perriault 50
  • 52. 30 Symfony Best Practices GOOD Nicolas Perriault 51
  • 53. 30 Symfony Best Practices #17 • There should never be any direct use of sfContext inside the Model layer (eg. sfContext::getInstance()) • Because the sfContext instance you get can differ a lot regarding the used env (cli, test, dev, prod...) • It would make your code quite untestable • Yes, it’s hard. Nicolas Perriault 52
  • 54. 30 Symfony Best Practices BAD Nicolas Perriault 53
  • 55. 30 Symfony Best Practices BAD Nicolas Perriault 54
  • 56. 30 Symfony Best Practices GOOD Nicolas Perriault 55
  • 57. 30 Symfony Best Practices GOOD Nicolas Perriault 56
  • 58. 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
  • 59. 30 Symfony Best Practices #19 • Use sfLogger for debugging instead of echoing variable or debug messages • FirePHP(1) or FireSymfony(2) custom loggers can be more than helpful to debug your app (webservices for example) (1) http://firephp.org/ (2) http://firesymfony.org/ Nicolas Perriault 58
  • 60. 30 Symfony Best Practices GOOD factories.yml The FireSymfony Firebug extension for Firefox in action Nicolas Perriault 59
  • 61. 30 Symfony Best Practices #20 • If source code is managed through a SCM tool, versioned files should NEVER contain passwords, local paths, etc. • Files containing them should be added to the ignore directive • Typical example: the databases.yml file • Distribute platform-dependent configuration files Nicolas Perriault 60
  • 62. 30 Symfony Best Practices BAD Nicolas Perriault 61
  • 63. 30 Symfony Best Practices GOOD Nicolas Perriault 62
  • 64. 30 Symfony Best Practices #21 • Write unit and functional tests: • Unit test symfony business logicit’s don’t test your or Doctrine again, (please already done) • Functionally test the user interface when it’s important (eg. 404/403/401 HTTP codes, security and user authentication, transactions and forms, etc.) Nicolas Perriault 63
  • 65. 30 Symfony Best Practices #21 Hint: you can write your own functional browser/ tester to avoid duplicate test code Nicolas Perriault 64
  • 66. 30 Symfony Best Practices #22 • Never use absolute paths in code, or at least put them in YAML configuration files. • Make your symfony project portable: use dirname(__FILE__) to point at the symfony libs in the project configuration class. • You can put symfony lib in a lib/vendor dir. Nicolas Perriault 65
  • 67. 30 Symfony Best Practices BAD Nicolas Perriault 66
  • 68. 30 Symfony Best Practices GOOD Nicolas Perriault 67
  • 69. 30 Symfony Best Practices #23 • Data objects should always be represented by a slug in urls, not a primary key • For security: numeric PKs are easily guessable, whereas slugs are not • For URL usability and SEO: /article/symfony-rulez.html is sexier than /article/123.html Nicolas Perriault 68
  • 70. 30 Symfony Best Practices #24 • Every application or project specific configuration variable should be stored in its app.yml file • You can share project-wide (cross- applications) settings by putting an app.yml file within the root config/ folder of the project Nicolas Perriault 69
  • 71. 30 Symfony Best Practices BAD Nicolas Perriault 70
  • 72. 30 Symfony Best Practices GOOD Default value Nicolas Perriault 71
  • 73. 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
  • 74. 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
  • 75. 30 Symfony Best Practices #26 Nicolas Perriault 74
  • 76. 30 Symfony Best Practices #27 • Alter request and response programmatically by using filters. • You can use the request.filter_parameters and response.filter_content events too. • Filters are easy to setup and to debug, whereas events are more clean and powerful. Matter of taste. Nicolas Perriault 75
  • 77. 30 Symfony Best Practices GOOD Nicolas Perriault 76
  • 78. 30 Symfony Best Practices GOOD Nicolas Perriault 77
  • 79. 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
  • 80. 30 Symfony Best Practices #29 • Share features and modules across applications by writing plugins • If it’s really cool, release it publicly Nicolas Perriault 79
  • 81. 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
  • 82. 30 Symfony Best Practices Questions? Nicolas Perriault nperriault@gmail.com +33 660 920 867 prendreuncafe.com | symfonians.net | symfony-project.org Nicolas Perriault