Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going bare - writing the web without a framework - php[world] 2018

Going bare - writing the web without a framework - php[world] 2018

This talk was given at php[world] 2018

If you've only ever experienced web development through a web framework, you're missing out. Have you ever been tempted to rewrite a codebase in your favorite framework? That's a smell that you need to come to this talk. Being comfortable developing without a framework is a crucial skill to have for refactoring legacy applications. In this talk I show you how to harness the power of going "frameworkless" so you'll feel empowered to make better decisions in your next web project. Be set free from your web-framework ball and chain.

Sammy Kaye Powers

November 15, 2018

More Decks by Sammy Kaye Powers

Other Decks in Programming


  1. Going Bare Writing the web without a framework Sammy Kaye

    Powers 2018-11-15
  2. Thanks to Our Sponsors 2018

  3. joind.in/talk/c3f16 @SammyK #phpworld Slides Get the joind.in/talk/c3f16

  4. joind.in/talk/c3f16 @SammyK #phpworld frameworks The point is not <3 frameworks!

  5. joind.in/talk/c3f16 @SammyK #phpworld opinion It’s OK to have an

  6. joind.in/talk/c3f16 @SammyK #phpworld experiment It’s OK to

  7. joind.in/talk/c3f16 @SammyK #phpworld Bootstrapping / ˈbo ͞ otˌstraˌpiNG / Define:

    (…at least for this talk) Code that acts as an entry point into an app.
  8. joind.in/talk/c3f16 @SammyK #phpworld Boilerplate / ˈboilərˌplāt / Define: The “glue”

    code that ties components together. (…at least for this talk)
  9. joind.in/talk/c3f16 @SammyK #phpworld Frameworks are a tool

  10. joind.in/talk/c3f16 @SammyK #phpworld Going bare is a skill (that you

    don’t get with a framework)
  11. joind.in/talk/c3f16 @SammyK #phpworld The of going bare skill • Writing

    bootstrap & boilerplate • Choosing exact components • Use different paradigms • Use those skills on domain code (Get to know the component ecosystem) (Like singleton, et al) (Gives you more control over how components interact) (Makes you better at writing your domain code)
  12. joind.in/talk/c3f16 @SammyK #phpworld Myths about going frameworkless

  13. joind.in/talk/c3f16 @SammyK #phpworld Myth #1 You must write everything from

  14. joind.in/talk/c3f16 @SammyK #phpworld Myth #2 You’re inherently anti-framework or anti-convention

  15. joind.in/talk/c3f16 @SammyK #phpworld Myth #3 You’ll spend most of your

    time on boilerplate instead of on domain code
  16. joind.in/talk/c3f16 @SammyK #phpworld Myth #4 You must be an architecture

  17. joind.in/talk/c3f16 @SammyK #phpworld Myth #5 You like pain

  18. joind.in/talk/c3f16 @SammyK #phpworld Pros Framework Cons

  19. joind.in/talk/c3f16 @SammyK #phpworld Pros

  20. joind.in/talk/c3f16 @SammyK #phpworld Pros

  21. joind.in/talk/c3f16 @SammyK #phpworld Pros crazy fast You get started

  22. joind.in/talk/c3f16 @SammyK #phpworld Pros First-time install time minutes! 5

  23. joind.in/talk/c3f16 @SammyK #phpworld Pros

  24. joind.in/talk/c3f16 @SammyK #phpworld Pros boilerplate all the things You don’t

    have to Con but it can be a
  25. joind.in/talk/c3f16 @SammyK #phpworld Pros bootstrap & boilerplate code You don’t

    have to maintain
  26. joind.in/talk/c3f16 @SammyK #phpworld Pros battle-tested The code is

  27. joind.in/talk/c3f16 @SammyK #phpworld Pros conventions Ready-to-follow Con but it can

    be a
  28. joind.in/talk/c3f16 @SammyK #phpworld Pros documentation Most frameworks have great

  29. joind.in/talk/c3f16 @SammyK #phpworld Pros marketable It makes you more

  30. joind.in/talk/c3f16 @SammyK #phpworld Pros pragmatic They are often

  31. joind.in/talk/c3f16 @SammyK #phpworld cons? What about the

  32. joind.in/talk/c3f16 @SammyK #phpworld Cons legacy codebases Useless for

  33. joind.in/talk/c3f16 @SammyK #phpworld Cons cruft They fill your project with

  34. joind.in/talk/c3f16 @SammyK #phpworld Cons Cruft • Deployment takes longer •

    Upgrading pulls in all the new features
  35. joind.in/talk/c3f16 @SammyK #phpworld Cons tightly couple Easier to due to

  36. joind.in/talk/c3f16 @SammyK #phpworld

  37. 5.3

  38. 5.7 2018-09-04

  39. None
  40. 5.7 Did not support laravelcollective/html mccool/laravel-auto-presenter

  41. None
  42. None
  43. joind.in/talk/c3f16 @SammyK #phpworld Cons upgrading Framework-specific deps can keep you

  44. joind.in/talk/c3f16 @SammyK #phpworld The The Cons Pros seem great are

  45. joind.in/talk/c3f16 @SammyK #phpworld Why would you go bare? (To answer

    we must ask…)
  46. joind.in/talk/c3f16 @SammyK #phpworld When should you go bare?

  47. joind.in/talk/c3f16 @SammyK #phpworld

  48. joind.in/talk/c3f16 @SammyK #phpworld Greenfield Legacy Depends

  49. joind.in/talk/c3f16 @SammyK #phpworld 1% 99% 90% 10% Go Bare Framwork

    Greenfield Legacy Going bare vs framework
  50. joind.in/talk/c3f16 @SammyK #phpworld How do you go bare?

  51. joind.in/talk/c3f16 @SammyK #phpworld from scratch You could just write

  52. joind.in/talk/c3f16 @SammyK #phpworld

  53. joind.in/talk/c3f16 @SammyK #phpworld That means writing all the things Router

    DBAL/ORM Dep Injection Logger
  54. joind.in/talk/c3f16 @SammyK #phpworld reinventing the wheel But that’s like

  55. joind.in/talk/c3f16 @SammyK #phpworld Better Way™ There’s a

  56. joind.in/talk/c3f16 @SammyK #phpworld po- nents Com-

  57. joind.in/talk/c3f16 @SammyK #phpworld components has a healthy ecosystem of

  58. joind.in/talk/c3f16 @SammyK #phpworld using them You’re probably already

  59. joind.in/talk/c3f16 @SammyK #phpworld built on them Most frameworks are zend-router

    zend-db zend-di zend-log
  60. joind.in/talk/c3f16 @SammyK #phpworld choose ur candy Going bare lets you

    Look at all those smarties! Yum!
  61. joind.in/talk/c3f16 @SammyK #phpworld I like smarties

  62. joind.in/talk/c3f16 @SammyK #phpworld Router DBAL/ORM Dep Injection Logger

  63. joind.in/talk/c3f16 @SammyK #phpworld zend-router fast-route php-router phroute Routers Tons of

  64. joind.in/talk/c3f16 @SammyK #phpworld Easily installed with

  65. joind.in/talk/c3f16 @SammyK #phpworld bag o’ candy A framework is like

    a Comes with licorice? Yuck!
  66. joind.in/talk/c3f16 @SammyK #phpworld I don’t like licorice

  67. joind.in/talk/c3f16 @SammyK #phpworld components When you go bare don’t just

    magically start working together
  68. joind.in/talk/c3f16 @SammyK #phpworld Key: Get comfy with bootstrapping & boilerplate

    code It’s really not that bad
  69. joind.in/talk/c3f16 @SammyK #phpworld examples Real-world

  70. joind.in/talk/c3f16 @SammyK #phpworld conventions When going bare, there aren’t any

    …so this is a way, not the way
  71. joind.in/talk/c3f16 @SammyK #phpworld Greenfield Let’s start with

  72. joind.in/talk/c3f16 @SammyK #phpworld Case study Raw CSV Calculate
 & save

    to DB Show report Client’s server Our server
  73. joind.in/talk/c3f16 @SammyK #phpworld Case study Download CSV & calculate CLI

    View the report HTTP Endpoint
  74. joind.in/talk/c3f16 @SammyK #phpworld coding domain in 5 mins Install framework

  75. joind.in/talk/c3f16 @SammyK #phpworld 61components installed before we code https://framework.zend.com/downloads

  76. joind.in/talk/c3f16 @SammyK #phpworld go micro We could… Slim Silex (et

  77. joind.in/talk/c3f16 @SammyK #phpworld Silex DEAD!

  78. joind.in/talk/c3f16 @SammyK #phpworld go bare! :) But let’s

  79. joind.in/talk/c3f16 @SammyK #phpworld Start in the CLI (Just like when

    you use a framework!)
  80. joind.in/talk/c3f16 @SammyK #phpworld 1. Make an empty composer.json or… $

    composer init
  81. joind.in/talk/c3f16 @SammyK #phpworld 2. Make a folder for domain code

    $ mkdir src
  82. joind.in/talk/c3f16 @SammyK #phpworld 3. Add autoloading $ composer \ dumpautoload

    And then run…
  83. joind.in/talk/c3f16 @SammyK #phpworld Now just install your components & tie

    them together
  84. joind.in/talk/c3f16 @SammyK #phpworld Key: Get comfy with bootstrapping & boilerplate

    code It’s really not that bad
  85. joind.in/talk/c3f16 @SammyK #phpworld Case study Download CSV & calculate CLI

    View the report HTTP Endpoint
  86. joind.in/talk/c3f16 @SammyK #phpworld Install a console component $ composer require

    symfony/console (or maybe you like zendframework/zend-console)
  87. joind.in/talk/c3f16 @SammyK #phpworld src/Console/Runner.php

  88. joind.in/talk/c3f16 @SammyK #phpworld We get to make our first bootstrap

  89. joind.in/talk/c3f16 @SammyK #phpworld run (sans .php extension) $ chmod +x

  90. joind.in/talk/c3f16 @SammyK #phpworld $ ./run

  91. joind.in/talk/c3f16 @SammyK #phpworld B.A.U. Now it’s just src/Console/Runner.php

  92. joind.in/talk/c3f16 @SammyK #phpworld src/Console/CalculateReportsCommand.php

  93. joind.in/talk/c3f16 @SammyK #phpworld $ ./run

  94. joind.in/talk/c3f16 @SammyK #phpworld

  95. joind.in/talk/c3f16 @SammyK #phpworld Congrats!

  96. joind.in/talk/c3f16 @SammyK #phpworld Install the DI container $ composer require

    pimple/pimple (Choose your poison)
  97. joind.in/talk/c3f16 @SammyK #phpworld Need to make singleton ready src/Application.php

  98. joind.in/talk/c3f16 @SammyK #phpworld Singleton Isn’t that an anti-pattern?

  99. joind.in/talk/c3f16 @SammyK #phpworld https://en.wikipedia.org/wiki/Singleton_pattern

  100. joind.in/talk/c3f16 @SammyK #phpworld

  101. joind.in/talk/c3f16 @SammyK #phpworld Singleton •Ensures one & only one instance

    •Global access (static function) •Initialization on first access
  102. joind.in/talk/c3f16 @SammyK #phpworld Fatal error: Call to private App\Application::__construct() src/Application.php

  103. joind.in/talk/c3f16 @SammyK #phpworld src/Application.php Global access + instantiate on first

  104. joind.in/talk/c3f16 @SammyK #phpworld Get an instance of Application

  105. joind.in/talk/c3f16 @SammyK #phpworld Prevent cloning src/Application.php

  106. joind.in/talk/c3f16 @SammyK #phpworld src/Application.php Prevent unserialization

  107. joind.in/talk/c3f16 @SammyK #phpworld •Ensures one & only one instance •Global

    access (static function) •Initialization on first access
  108. joind.in/talk/c3f16 @SammyK #phpworld Case study Download CSV & calculate CLI

    View the report HTTP Endpoint
  109. joind.in/talk/c3f16 @SammyK #phpworld We get to make our final bootstrap

  110. joind.in/talk/c3f16 @SammyK #phpworld public/index.php

  111. joind.in/talk/c3f16 @SammyK #phpworld Eh?

  112. joind.in/talk/c3f16 @SammyK #phpworld src/Application.php However you register service providers into

    the container (different DI containers have their own API)
  113. joind.in/talk/c3f16 @SammyK #phpworld Why not in constructor?

  114. joind.in/talk/c3f16 @SammyK #phpworld src/Application.php

  115. joind.in/talk/c3f16 @SammyK #phpworld Install a router $ composer require nikic/fast-route

    (Choose your poison)
  116. joind.in/talk/c3f16 @SammyK #phpworld service provider Make a routing Pimple syntax

    - mileage my vary
  117. joind.in/talk/c3f16 @SammyK #phpworld Register in container src/Application.php

  118. joind.in/talk/c3f16 @SammyK #phpworld src/Router.php

  119. joind.in/talk/c3f16 @SammyK #phpworld Ugly A.F. src/routes.php

  120. joind.in/talk/c3f16 @SammyK #phpworld Different project, same component src/routes.php

  121. joind.in/talk/c3f16 @SammyK #phpworld public/index.php run We got bootstrap Runs CLI

    Runs HTTP
  122. joind.in/talk/c3f16 @SammyK #phpworld We got boilerplate src/Router.php src/Console/Runner.php symfony/console nikic/fast-route

  123. joind.in/talk/c3f16 @SammyK #phpworld Still not convinced?

  124. joind.in/talk/c3f16 @SammyK #phpworld What about Legacy

  125. joind.in/talk/c3f16 @SammyK #phpworld Why would you go bare? It’s a

    skill you can use for legacy apps
  126. joind.in/talk/c3f16 @SammyK #phpworld I’ll just rewrite this in x framework…

  127. joind.in/talk/c3f16 @SammyK #phpworld Totes samezies except routing

  128. joind.in/talk/c3f16 @SammyK #phpworld Route exists? Not found Router Yes No

    Code 404
  129. joind.in/talk/c3f16 @SammyK #phpworld Route exists? Legacy router New router Yes

    No New code
  130. joind.in/talk/c3f16 @SammyK #phpworld app/webroot/index.php Old CakePHP bootstrap file

  131. joind.in/talk/c3f16 @SammyK #phpworld src/bootstrap.php Same as public/index.php for HTTP

  132. joind.in/talk/c3f16 @SammyK #phpworld src/Router.php

  133. joind.in/talk/c3f16 @SammyK #phpworld tests? What about

  134. joind.in/talk/c3f16 @SammyK #phpworld Install PHPUnit $ composer require phpunit/phpunit --dev

  135. joind.in/talk/c3f16 @SammyK #phpworld phpunit.xml

  136. joind.in/talk/c3f16 @SammyK #phpworld tests/bootstrap.php

  137. joind.in/talk/c3f16 @SammyK #phpworld src/Application.php

  138. joind.in/talk/c3f16 @SammyK #phpworld Run the test suite $ ./vendor/bin/phpunit

  139. joind.in/talk/c3f16 @SammyK #phpworld How to choose your components

  140. joind.in/talk/c3f16 @SammyK #phpworld Use more than one framework #1

  141. joind.in/talk/c3f16 @SammyK #phpworld Search packagist.org #2 (Make sure it has

    good docs & is actively maintained)
  142. joind.in/talk/c3f16 @SammyK #phpworld experiments Try some

  143. joind.in/talk/c3f16 @SammyK #phpworld 100% middleware My next experiment: (I could

    totes use Zend Expressive or just go bare)
  144. joind.in/talk/c3f16 @SammyK #phpworld pros? framework ‘member the

  145. joind.in/talk/c3f16 @SammyK #phpworld Framework pros Crazy fast to get started

    …but you’ll get faster at going bare
  146. joind.in/talk/c3f16 @SammyK #phpworld Framework pros to maintain …most the time

    it’s really not that bad No bootstrap & boilerplate code
  147. joind.in/talk/c3f16 @SammyK #phpworld Framework pros The code is …so are

    the components you use when going bare battle tested
  148. joind.in/talk/c3f16 @SammyK #phpworld Framework pros Great …so are the components

    you use when going bare documentation
  149. joind.in/talk/c3f16 @SammyK #phpworld Framework pros Makes you more …going bare

    is a marketable skill marketable
  150. joind.in/talk/c3f16 @SammyK #phpworld Framework pros They are often …in many

    contexts going bare is more pragmatic pragmatic
  151. joind.in/talk/c3f16 @SammyK #phpworld myths? Did we bust the

  152. joind.in/talk/c3f16 @SammyK #phpworld #1 You must write everything from scratch

    Nope, use components
  153. joind.in/talk/c3f16 @SammyK #phpworld #2 You’re inherently anti- framework or anti-convention

    Nope, frameworks are great for most greenfield projects
  154. joind.in/talk/c3f16 @SammyK #phpworld #3 You’ll spend most of your time

    on bootstrapping instead of on domain code It takes a little longer at first but you’ll get faster with practice
  155. joind.in/talk/c3f16 @SammyK #phpworld #4 You must be an architecture astronaut

    Going bare is more pragmatic for legacy codebases
  156. joind.in/talk/c3f16 @SammyK #phpworld #5 You like pain No, we just

    like to improve our skills
  157. joind.in/talk/c3f16 @SammyK #phpworld

  158. joind.in/talk/c3f16 @SammyK #phpworld Remember: It’s all about tradeoffs.

  159. joind.in/talk/c3f16 @SammyK #phpworld Resources

  160. joind.in/talk/c3f16 @SammyK #phpworld Create a PHP application without a framework

    Patrick Louys https://github.com/PatrickLouys/no- framework-tutorial
  161. joind.in/talk/c3f16 @SammyK #phpworld Refactoring Martin Fowler

  162. joind.in/talk/c3f16 @SammyK #phpworld <3 frameworks I I also <3 my

    skill for Going Bare
  163. Sammy Kaye Powers Thanks! /talk/c3f16 @SammyK SammyK.me Host of @PHPRoundtable