Silex, the micro-framework
@blongden
symfony/http-foundation
symfony/event-dispatcher
symfony/routing
symfony/http-kernel
pimple/pimple
Why Silex?
alias getcomposer='curl -s https://getcomposer.org/installer | php'
alias composer='php composer.phar'
Routes
Method, Pattern & Closure
Dynamic Routes
Converting Parameters
Route Assertions
Providers
• Service Providers
• Controller Providers
•   DoctrineServiceProvider

•   MonologServiceProvider

•   SessionServiceProvider

•   SwiftmailerServiceProvider

•   TwigServiceProvider

•   TranslationServiceProvider

•   UrlGeneratorServiceProvider

•   ValidatorServiceProvider

•   HttpCacheServiceProvider

•   FormServiceProvider

•   SecurityServiceProvider
Twig Templates
Traits
Forms
Handling POST
UrlGeneratorServiceProvider
View the guestbook
Http Cache
Edge Side Includes
<esi:include src=”/message” />
https://github.com/blongden/silex-guestbook
https://joind.in/6948
wallcoo.net
                   http://www.flickr.com/photos/8ballofwakefield/513907976/
           http://weblogs.wxmi.com/news/traffic/dirty-windshield/Highway%20signs.JPG
                             http://plaza.ufl.edu/dianis/images/dna.jpg
http://4.bp.blogspot.com/-ucuY9INjqrI/TajciAM3V_I/AAAAAAAAClY/EgdsT_i01Jg/s1600/P1040125.JPG
              http://www.greatgiddingschool.co.uk/wpimages/wpa87715ae_05_06.jpg
        http://upload.wikimedia.org/wikipedia/commons/5/5b/Cache_Memory-jurvetson.jpg
             http://img1.etsystatic.com/003/0/6263865/il_fullxfull.361156505_ghgv.jpg

Silex, the microframework

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 This talk contains a lot of code\nKeep up!\n
  • #8 OO layer for the HTTP specification\nRequest and Response object\nUploaded files, cookies &amp; sessions\n
  • #9 Lightweight implementation of the Observer design pattern\naddListener with event\ndispatch event\n
  • #10 Associates a request with the code that will convert it to a response\n
  • #11 The engine for Silex and Symfony\nThe building blocks for building a framework\n- Reverse Proxy with caching and ESI\n- Exception Listener\n- Functional Testing\nSimple interface to turn a request into a response\n
  • #12 Simple Dependency Injection Container\n50 lines of code\n
  • #13 You COULD use Plain Old PHP.\nSingle Page apps\n- Concise\n- Extensible via Pimple\n- Testable abstract Request and Response\n
  • #14 \n
  • #15 \n
  • #16 Easy installation\n
  • #17 Minimum config required to get you up and running\n
  • #18 A tip for people who use composer a lot\n
  • #19 Symfony dependencies and versions\n
  • #20 Lock file (install specific version)\nAutoload file which we use in our code\n
  • #21 Minimum bootstrap\nSet&amp;#x2019;s composers autoloader and sets up Silex\n
  • #22 PHP Development server\nNo apache required before we get started!\n
  • #23 No controllers, so all requests will 404.\n
  • #24 \n
  • #25 \n
  • #26 How the framework translates your request into the code that is executed\nEffectively maps Request data to Code\n
  • #27 Request data &amp; Code\nMethod = HTTP Method\nPattern = URL path\nClosure (anoymous function) = the code\n
  • #28 Closure returns a string, or a response object\nGET, POST, PUT, DELETE\n\n
  • #29 \n
  • #30 \n
  • #31 This route will only match a Url *with* a name supplied\nParam is passed into the function\n
  • #32 The default allows it to match / and optionally {name}\n
  • #33 \n
  • #34 The results of the convert function passed to Closure\nThis could be an object of a class, for example.\n
  • #35 Allow us to add arbitrary conditions to a route before it will match\n
  • #36 Contrived example! 2 routes with asserts\n/word matched the first route\n/4 matches the second\n
  • #37 Key part of the provided functionality in Silex\n\n
  • #38 Service providers are a way of reusing code across controllers\nController Providers are a way of reusing Controllers themselves\n
  • #39 These are provided\nThird party to provide Propel, Redis, Mongo, alternative templating engines, Config (XML/JSON/YAML), Gravatar, Memcache, Solr etc.\n
  • #40 Silex supports Twig out the box\n
  • #41 A few more dependencies - silex is very light on its own\ntwig-bridge optional but adds some convenience methods to Twig\n - path and url for UrlGeneratorServiceProvider\n - trans and transchoice for TranslationServiceProvider\n - set of methods for handling forms in templates (which is what we are using)\n - is_granted for SecurityServiceHelper\n
  • #42 Pass a directory for where the templates can be found\nWe&amp;#x2019;re not actually using twig at this point - just registering\n
  • #43 \n
  • #44 \n
  • #45 Is everyone familiar with Traits in PHP 5.4?\n&amp;#x201C;compiler assisted copy &amp; paste&amp;#x201D;\n
  • #46 All built-in providers have traits to enhance the Application object\nShort array syntax since we know we&amp;#x2019;re using 5.4!\n
  • #47 \n
  • #48 We add two line to our dependencies - symfony/form and symfony translation.\n
  • #49 The translation provider is required for the default form layout\nSymfony forms are a talk on there own and there&amp;#x2019;s docs out there\nSplit the services and traits out into a bootstrap file\n
  • #50 This looks like too much controller logic\n
  • #51 We don&amp;#x2019;t like fat controllers...\n
  • #52 Refactor form code\nmake it more re-usable\n
  • #53 Reduce code in controllers\n
  • #54 form_widget is a twig helper from the symfony bridge\n
  • #55 Until the invention of twitter bootstrap all my forms looked like this\n
  • #56 Much better\nExcept now all my forms look like this\n
  • #57 Any old enough to remember the 1990&amp;#x2019;s &amp;#x2018;guestbooks&amp;#x2019; on websites\n
  • #58 In handling POST we need to identify the target URL\n\n
  • #59 We do this with another service provider\nAnd an optional Trait\n
  • #60 We&amp;#x2019;re using the trait and registering the provider\n
  • #61 We use bind to give this route a name, &amp;#x2018;homepage&amp;#x2019;.\n
  • #62 Template grown a little since the last time we saw it\nEssentially the only change is the form action\n
  • #63 Update the method to match\nAdd methods to match to method\nAdd code to validate and save the new entry\nRecap!\n
  • #64 This creates a guestbook and adds the new entry\nProblem with instantiating the model in the controller\nWe need to create a service to supply the guestbook\n
  • #65 This is a shared service\nFirst time it&amp;#x2019;s accessed it will create the object\nOnce created it returns the same object\nlazy loading\n
  • #66 We then refactor code to make use of the service we defined\nReduce controller responsibility\n
  • #67 \n
  • #68 Set up a new route with the code to display the messages list\n
  • #69 \n
  • #70 The render function allows us to use another controller\nThis is excellent for apps using AJAX as calling this route will return the HTML block\n
  • #71 \n
  • #72 Caches are the reason the web is able to scale\nBrowser caches are good but server cache is fun!\nSymfony has a built in reverse proxy with support for ESI\nOr we can use varnish\n\n
  • #73 Small markup language for edge level dynamic content assembly\n
  • #74 Inserted into HTML or any text based content as creation\nInterpreted by an intermediary\nAllow for blocks of code to be independently cached from the main page\n
  • #75 Because no presentation is complete without a picture of a cloud...\nSymfony components can handle Server Cache and Application as part of the same app\n
  • #76 This is the version that caused Silex to render the result of the messages controller\nA small change required to use ESI\n
  • #77 We simply update the markup so that the http output contains the ESI directive\n
  • #78 Set the cache directory to store static HTML\nSet debug so we can see the headers\n
  • #79 Set TTL on responses and Surrogate-Control header for proxy\nrun the http_cache rather than $app-&gt;run\nDEMO!\n
  • #80 \n
  • #81 Go build sandcastles!\n
  • #82 \n