The Great Framework Kumbaya

F5dfeeef276fcfd4751f4063487a5a3f?s=47 weaverryan
October 31, 2014

The Great Framework Kumbaya

Time to gather our frameworks around a warm camp fire and see just how much we have in common In this presentation, we take a flat PHP4-style application and gently migrate it into our own "framework", that uses components from Symfony2, Zend Framework 2, Aura and a library called Pimple. By the end, you'll see how any ugly application can take advantage of the many wonderful tools available to PHP developers. You'll also learn exactly how *any* framework actually works under the hood by hooking up routing, controllers, services and events.

F5dfeeef276fcfd4751f4063487a5a3f?s=128

weaverryan

October 31, 2014
Tweet

Transcript

  1. by your friend: ! Ryan Weaver @weaverryan The Great Framework

    Kumbaya
  2. KnpUniversity.com github.com/weaverryan Who is this Happy Camper? > Lead for

    the Symfony documentation
 > KnpLabs US - Symfony Consulting, training, Kumbaya ! > Writer for KnpUniversity.com Tutorials > Husband of the much more talented @leannapelham
  3. ! Personal Homepage

  4. None
  5. None
  6. Yep, that’s a sausage hat wink wink that’s me! @jmikola

  7. http://bit.ly/php-camp @weaverryan

  8. Open our Database connection // index.php

  9. Try to get a clean URI // index.php

  10. Render the homepage // index.php

  11. List our happy campers! // index.php

  12. Why don’t we code like this anymore?

  13. The Tribes of PHP meet the UN

  14. Formerly Solar

  15. > The United Nations of PHP ! > Equally Important

    ! > Equally dysfunctional (like any happy family) @weaverryan
  16. None
  17. FIG != collaboration

  18. FIG Empowers You…

  19. To become best friends with all the frameworks

  20. A) Reading request info could be better ! B) can’t

    set headers later on ! C) front controller ! D) little URL control ! E) all objects are global ! F) 1 giant file (no separation) ! G) HTML and PHP is mixed! Problems @weaverryan
  21. ! $_SERVER[‘HOST'] ! header('X-POWERED-BY: crap');

  22. None
  23. None
  24. composer require symfony/http-foundation … used by Laravel, Silex, Symfony, Drupal

    8…
  25. Current Status @weaverryan

  26. WOW!

  27. ... becomes ... The “Request” object

  28. ... becomes ... The “Request” object

  29. ... becomes ... The “Response” object

  30. ! if ($url == '/') {

  31. if ($uri == '/' || $uri == '') { //

    ... } elseif ($uri == '/attendees') { // ... } else { // ... } @weaverryan Our app is a giant gross “if” statement Problems Grabbing a piece from the URL like /blog/my-blog-post will take some work
  32. Aura.Router

  33. composer require aura/router First composer require

  34. Then Celebrate!

  35. @weaverryan Current Status

  36. 1) Map URI to “controller”

  37. None
  38. None
  39. None
  40. 2) Execute the controller* * each controller is a flat

    function
  41. The Controllers

  42. $kitten--

  43. 1. Request cleans the URI 2. Router matches the URI

    to a route, returns a 
 “controller” string 3. We execute the controller function 4. The controller creates a Response object 5. We send the Response headers and content The Big Picture @weaverryan
  44. our 20 line framework solves…

  45. A) Reading request info could be better ! B) can’t

    set headers later on ! C) front controller ! D) little URL control ! E) all objects are global ! F) 1 giant file (no separation) ! G) HTML and PHP is mixed! Problems @weaverryan
  46. ! Global objects are universally hated

  47. > We’ve got lots of random, disorganized objects floating around

    and some depend on others Problems > And we can’t easily access them from within our controllers @weaverryan
  48. Pimple! … used by Silex … name loved by ???

  49. composer require pimple/pimple First composer require

  50. Then Celebrate!

  51. Centralize the db connection

  52. Centralize the db connection

  53. Pass the container to the controller

  54. $kitten++

  55. Container all the things!

  56. None
  57. None
  58. With everything in the container, our “framework” just got skinny

  59. None
  60. None
  61. ! Tackling Disorganization

  62. We have 1 file with everything mixed ! ! 1)

    Creation of container & objects 2) Definition of routes 3) Definition of controllers 4) The “framework” code that executes everything Problems @weaverryan
  63. We have 1 file with everything mixed ! 1) services.php

    2) routes.php 3) controllers.php 4) bootstrap.php (framework code) Basic Solution @weaverryan
  64. index.php Nothing to see here...

  65. bootstrap.php …

  66. services.php

  67. routing.php

  68. controllers.php

  69. bootstrap.php … all the same “stuff”…

  70. All Frameworks Work Like This

  71. None
  72. None
  73. ! <h1><?php $crap; ?></h1>

  74. Problems @weaverryan bleh!

  75. composer require twig/twig First composer require

  76. Then Celebrate!

  77. Before

  78. After

  79. views/attendees.twig

  80. Getting kinda easy, right?

  81. What’s available? Search GitHub!

  82. All Your Frameworks Are Belong to Us

  83. To focus on the interesting problems… ! (business problems)

  84. … we need to remove the boring, repetitive problems…

  85. … but don’t limit yourself to one “world”… Symfony Developer

    Laravel Developer Drupal Developer Silex Developer
  86. … be a PHP Developer

  87. Thanks! Ryan Weaver @weaverryan KnpUniversity.com Symfony, Behat, Twig, OO, etc

    Tutorial Screencasts https://joind.in/11898