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

Composing PHP Applications with Middleware (PHP World 2016)

Josh Butts
November 16, 2016
97

Composing PHP Applications with Middleware (PHP World 2016)

Josh Butts

November 16, 2016
Tweet

Transcript

  1. About Me • VP of Engineering, offers.com • Austin PHP

    Organizer • Play competitive Skee Ball • github.com/jimbojsb • @jimbojsb 2
  2. What is PSR-7 • FIG standard for HTTP messages •

    A collection of interfaces • By itself, has nothing specific to do with middleware 4
  3. But first - More History • 2014 - everyone* has

    gone out and built and object model of HTTP • Hello World for writing a framework • None of them are compatible • Symfony\HttpFoundation widely used 10
  4. What is middleware? • Turns a Request into a Response

    • Stackable • Reusable • Dispatachable 14
  5. PHP Copied This Trend • Node.js Connect • Ruby Rack

    • Python WSGI • Even StackPHP 15
  6. Express.js is getting it right • All middleware all the

    way down • Even error handlers are middleware • Even the app instance itself is middleware 16
  7. Components of a PHP Middleware App • PSR-7 implementation •

    Collection of middleware • Middleware dispatcher • Any other libraries that will actually do business logic for you 17
  8. Pick a PSR-7 Implementation • Do not write one •

    Unless you’re using Slim already just use Zend/Diactoros 19
  9. What does a middleware look like? 20 <?php $middleware =

    function( \Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response, callable $next) { // do the actual stuff };
  10. It might also look like this 21 <?php $middleware =

    function( \Psr\Http\Message\RequestInterface $request, callable $next) { // do some stuff };
  11. PSR-15 • There is still some debate as to which

    is better • It’s likely the standard will not pass along the response • Lots of existing middleware out there that would not meet this spec 22