Get Started, Move Fast & Break things in Symfony2

F5dfeeef276fcfd4751f4063487a5a3f?s=47 weaverryan
February 08, 2014

Get Started, Move Fast & Break things in Symfony2

Grab some coffee and let's get your project moving on Symfony2! Since its 2.0 release in 2011, a lot of fantastic tools and best-practices have been built up around Symfony. In this talk, we'll start a real project and learn about some of these tools that can make us dangerous as quickly as possible! Symfony2 lets you write beautiful, maintainable code, and I'll give you 5 tips to do just that while staying aggressive with getting features out the door. RAD!

F5dfeeef276fcfd4751f4063487a5a3f?s=128

weaverryan

February 08, 2014
Tweet

Transcript

  1. PHP Tutorial Screencasts Get Started, Move Fast & Break things

    in Symfony2
  2. 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
  3. Success!

  4. Success?

  5. How do we define it?

  6. http://www.flickr.com/photos/martin_heigan/4544138976/ A I delivered the Feature on time?

  7. I came in under budget? B http://www.flickr.com/photos/68751915@N05/6355360253/

  8. I delivered exactly what the client asked for? C http://www.flickr.com/photos/adesigna/4090782772/

  9. I delivered exactly what the client asked for? needed C

    http://www.flickr.com/photos/adesigna/4090782772/
  10. D The app has XX% test coverage? http://www.flickr.com/photos/sidelong/246816211/

  11. E My controllers average 6.35 lines? http://www.flickr.com/photos/dharder9475/5381826270/

  12. F I use Symfony2 with 55 Bundles! http://www.flickr.com/photos/sallypics/60614323/

  13. Which one is it?

  14. Define good… - BDD/TDD is *good*, right? - Skinny controllers

    are definitely good… I think? - 80%+ test coverage - is that good?
  15. We measure success entirely subjectively

  16. thus…

  17. Best practices & tools for “success” are subjectively decided

  18. We can aim for better!

  19. Measuring Success X*Quality - (1-X)*Speed = Success Score ! where

    X is 0-1, related to many factors X = the Quality factor
  20. 1. Uncertainty 2. Mission Critical Features 3. Budget X-Factors

  21. 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
  22. 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
  23. 1. No budget? No choice, fast! 2. No budget =>

    low X (less quality, faster!) Budget
  24. Let’s build *success*

  25. Success: ! Find what it means for *your* project and

    hit it!
  26. Writing a lot of tests may mean lower success

  27. Or it may save yer arse

  28. Or it may save yer arse @grmpyprogrammer … from him

  29. (Technical) Debt is not bad

  30. try buying a house without debt

  31. Manage and leverage technical debt

  32. RAD in Symfony2

  33. Symfony2 lends naturally to high quality apps (high X)

  34. How can we go fast? http://www.flickr.com/photos/curtisperry/141526923/

  35. 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
  36. 1 http://hyperboleandahalf.blogspot.com/2010/06/this-is-why-ill-never-be-adult.html Shorten all the Things

  37. A bundle called “App”

  38. Brought to you by:

  39. 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”
  40. a 2 Routing http://www.flickr.com/photos/14617207@N00/4471342864/

  41. A Routing File Bundle name Controller

  42. router:debug

  43. 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
  44. None
  45. 3 Querying for Data

  46. Automatically Queried

  47. ! 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
  48. None
  49. A Routing File teams is available as an argument to

    any method in the controller
  50. 1. SensioFrameworkExtraBundle doesn’t work to query for an array of

    objects 2. “teams” variable can be used across all actions KnpRadBundle Resources
  51. 4 Rendering the Template

  52. Will Render App:Team:show.html.twig App\Controller\TeamController::showAction

  53. But wait, the template doesn’t exist yet!!!

  54. None
  55. 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
  56. 5 Fixtures! http://www.flickr.com/photos/ciordia/8162313/

  57. AppBundle/Resources/fixtures/fixtures.yml

  58. Brought to you by: Alice & Faker https://github.com/nelmio/alice https://github.com/fzaninotto/Faker

  59. AppBundle/Resources/fixtures/fixtures.yml Create 50 users

  60. AppBundle/Resources/fixtures/fixtures.yml Create 50 users Create 50 users Faker gives you

    fake data functions
  61. AppBundle/Resources/fixtures/fixtures.yml Create 50 users Create 50 users Associate with a

    random team Faker gives you fake data functions
  62. Load any Resources/fixtures/*.yml file easily with DoctrineFixturesBundle

  63. None
  64. 6 Forms! http://www.flickr.com/photos/theworldisatwarwegonnastopit/4443672898/

  65. None
  66. Creates and binds the form

  67. Creates and binds the form Creates and binds the form

    Shortcut methods on KnpRadBundle base Controller
  68. 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
  69. 7 Form Theming!

  70. 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
  71. 8 Service Configuration!

  72. 1. Normally done with a services.yml or services.xml 2. KnpRadBundle

    automatically loads a Resources/config/services.yml Service Configuration
  73. JMSDiExtraBundle allows annotation configuration

  74. None
  75. 1. JmsDiExtraBundle saves you a little bit of time, but

    I don’t love it 2. Seems to have cache issues JmsDiExtraBundle?
  76. Security 9 http://www.flickr.com/photos/carbonnyc/2294144289/

  77. None
  78. Check for user roles

  79. Check for user roles Custom object permissions (with voters)

  80. SensioFrameworkExtraBundle ! 1. Flexible by using voters to hold complex

    authorization logic 2. Easily add authorization and scan for it later
  81. Special Services 10

  82. Certain classes are auto-registered into the service container by convention

    KnpRadBundle
  83. 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
  84. Using the auto-registered repository service

  85. 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
  86. Part 3: ! Iterating Quickly

  87. Lets learn from our experiments quickly and adjust

  88. 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
  89. 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
  90. 3 Behat Behat converts human-readable 
 descriptions of a feature

    into a 
 functional test
  91. Behat team.feature file

  92. None
  93. 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
  94. 4 CI / Travis 1. Develop quickly without worrying about

    tests 2. Always know if your app is in a deployable state
  95. 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
  96. It’s basically ridiculous

  97. 1. RADBundle developer 2. Professional nice guy 3. Helped with

    this presentation 6 Tips from Florian @docteur_klein
  98. 6 Abstraction @docteur_klein Florian says: Don’t over engineer with abstraction

    layers (YAGNI)
  99. Maybe you need Silex not Symfony

  100. 6 Small Classes @docteur_klein Florian says: Create many small classes

    (single responsibility principle)
  101. 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
  102. Final Act ! Break (some) things

  103. 1. Define your success metric A. How much uncertainty? B.

    Mission critical features C. Did we find any money? RAD
  104. 2. Choose your RAD Features • KnpRadBundle • SensioFrameworkExtraBundle •

    MopaBootstrapBundle • Alice+Faker • SonataAdminBundle • APYDataGridBundle • … RAD
  105. 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
  106. Learn Faster

  107. Make a more *successful* project

  108. https://joind.in/10509 PHP Tutorial Screencasts Ryan Weaver @weaverryan

  109. @weaveryan @KnpUniversity https://joind.in/10509 https://github.com/knpuniversity/sunshinephp-rad