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

Zend Expressive Microservice Framework Blastoff

Adam Culp
October 15, 2016

Zend Expressive Microservice Framework Blastoff

With PHP frameworks being more decoupled than ever, and with the help of package and dependency managers, large and heavy PHP frameworks are a thing of the past. Modern PHP developers now have a wealth of libraries available specializing at certain tasks, and microservices are fast becoming a preferred way to architect applications. But many don't know how to start, and get thrown in the deep end.

This talk will briefly introduce what microservices are, and how to create them using middleware. Then show how to build using the Zend Expressive microframework leveraging components of Zend Framework, and other libraries, to quickly create awesome things without requiring a full stack framework. Resources for reference and continued learning will also be shared.

Adam Culp

October 15, 2016
Tweet

More Decks by Adam Culp

Other Decks in Programming

Transcript

  1. 2 Expressive Microservice Framework Blastoff • About me – OSS

    Contributor – PHP Certified – Zend Certification Advisory Board – PHP-Fig voting member (IBM i Toolkit) – Consultant at Zend Technologies – Organizer SoFloPHP (South Florida) – Organizer SunshinePHP (Miami) – Long distance (ultra) runner – Photography Enthusiast – Judo Black Belt Instructor
  2. 3 Expressive Microservice Framework Blastoff • About me – OSS

    Contributor – PHP Certified – Zend Certification Advisory Board – PHP-Fig voting member (IBM i Toolkit) – Consultant at Zend Technologies – Organizer SoFloPHP (South Florida) – Organizer SunshinePHP (Miami) – Long distance (ultra) runner – Photography Enthusiast – Judo Black Belt Instructor PHP Ninja!!!
  3. 5 Expressive Microservice Framework Blastoff • Frameworks Suck – Complicated

    • Routing • Databases • Connectivity • Communication (HTTP, API) • Information Container • GUI (html, javascript, templates, CSS,) • Errors and Exceptions • Validation and Cleansing Data • State
  4. 6 Expressive Microservice Framework Blastoff • Frameworks Web Applications Suck

    – Complicated • Routing • Databases • Connectivity • Communication (HTTP, API) • Information Container • GUI (html, javascript, templates, CSS,) • Errors and Exceptions • Validation and Cleansing Data • State
  5. 8 Expressive Microservice Framework Blastoff • Microservice – All the

    buzz is “microservices”. – ...complex applications are composed of small, independent processes communicating with each other using language-agnostic APIs. These services are small building blocks, highly decoupled and focused on doing a small task, facilitating a modular approach to system-building. – Wikipedia
  6. 9 Expressive Microservice Framework Blastoff • But in PHP... –

    How to keep microservices light? – Microservices shouldn’t be heavy I’m a Microservice!!!
  7. 10 Expressive Microservice Framework Blastoff • Full Stack Frameworks Suck

    – Heavy and bloated – “Kitchen Sink” – “You don’t have to use everything, but its there...”
  8. 11 Expressive Microservice Framework Blastoff • Need For Speed –

    What does a microservice “need”? • HTTP message layer • Routing capabilities • Dependency injection – Testable – Swappable pieces • Templating – Optional (APIs may not need it, except documentation)
  9. 12 Expressive Microservice Framework Blastoff • PHP Ecosystem Facilitators –

    PHP 7 – Microframeworks – Libraries – Components – Containers – Composer
  10. 13 Expressive Microservice Framework Blastoff • All The Things!!! –

    So many tools: Monolog Whoops Flysystem IBMiToolkit OAuth2 Server https://github.com/ziadoz/awesome-php
  11. 14 Expressive Microservice Framework Blastoff • Communication Sucks – Say

    what!?! Monolog Whoops Flysystem IBMiToolkit OAuth2 Server
  12. 15 Expressive Microservice Framework Blastoff • PSR-7 Doesn’t Suck –

    Part of PHP-Fig.org recommendations – HTTP Messages • Request from client to server • Response from server to client – Interfaces • Psr\Http\Message\MessageInterface – Psr\Http\Message\RequestInterface • Psr\Http\Message\ServerRequestInterface – Psr\Http\Message\ResponseInterface • Psr\Http\Message\StreamInterface • Psr\Http\Message\UploadFileInterface • Psr\Http\Message\UriInterface
  13. 16 Expressive Microservice Framework Blastoff • Middleware – ...Middleware makes

    it easier for software developers to implement communication and input/output, so they can focus on the specific purpose of their application. – Wikipedia – Lighter applications (only what is needed) – Composed of layers
  14. 17 Expressive Microservice Framework Blastoff • Zend Expressive – Microframework

    built around middleware – Very lean runtime – Built to consume PSR-7 – Use for building: • APIs • Web applications • Single page sites – Choose your own stack – Great documentation • https://zendframework.github.io/zend-expressive/
  15. 18 Expressive Microservice Framework Blastoff • Composer Install Script –

    Done right from the start All code available at: https://github.com/adamculp/expressive-blastoff
  16. 20 Expressive Microservice Framework Blastoff • Composer Install Script –

    Minimal skeleton or full*? • With or without samples
  17. 21 Expressive Microservice Framework Blastoff • Composer Install Script –

    Router options • Aura.Router • FastRoute* • Zend Router
  18. 22 Expressive Microservice Framework Blastoff • Composer Install Script –

    Container options: (container interop) • Aura.Di • Pimple • Zend ServiceManager*
  19. 23 Expressive Microservice Framework Blastoff • Composer Install Script –

    Template engine options: • Plates • Twig • Zend View • None*
  20. 30 Expressive Microservice Framework Blastoff • Container Creation – We

    specified Zend ServiceManager (/config/container.php)
  21. 32 Expressive Microservice Framework Blastoff • Load Dependencies – Items

    to be called as middleware in routes. (/config/autoload/routes.global.php)
  22. 33 Expressive Microservice Framework Blastoff • Load Routes – Matches

    path to middleware (Dependencies shown earlier) (/config/autoload/routes.global.php cont’d)
  23. 34 Expressive Microservice Framework Blastoff • Action Anatomy – Creates

    raw JSON response (/src/App/Action/PingAction.php)
  24. 37 Expressive Microservice Framework Blastoff • Header Middleware – Create

    class for new middleware (or include someone else’s)
  25. 38 Expressive Microservice Framework Blastoff • Header Middleware – The

    class to include the very important header (/src/App/Middleware/TheClacksMiddleware.php)
  26. 39 Expressive Microservice Framework Blastoff • Header Middleware – Add

    the middleware to the container – Set it to always be included (/config/autoload/middleware-pipeline.global.php)
  27. 42 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – First we need a database connection. • Will use Zend-Db for this example, but could be anything. • Composer to the rescue!
  28. 43 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Specify adapter (provided by Zend/Db/ConfigProvider() in this case) (/config/autoload/db.global.php)
  29. 44 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Provide local/instance configuration • This would be driver and credentials • (credentials not needed with sqlite) (/config/autoload/db.local.php)
  30. 45 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Add the new action to dependencies (/config/autoload/routes.global.php)
  31. 46 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Add the new route to dependencies (/config/autoload/routes.global.php cont’d)
  32. 47 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Create a factory to pass items needed by the action (/src/App/Action/UserListFactory.php)
  33. 48 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Create the action (view 1 of 2 - constructor) (/src/App/Action/UserListAction.php)
  34. 49 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Create the action (view 2 of 2 - __invoke method) (/src/App/Action/UserListAction.php cont’d)
  35. 50 Expressive Microservice Framework Blastoff • Database Connected Example With

    Zend Db – Create the view template (/templates/app/user-list.phtml)
  36. 53 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – First we need a database connection. • Will use Doctrine DBAL for this example, but could be anything. • Composer to the rescue!
  37. 54 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – Provide local/instance configuration • This would be driver and credentials • (credentials not needed with sqlite) (/config/autoload/dbal.local.php)
  38. 55 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – Add the new route to dependencies (/config/autoload/routes.global.php)
  39. 56 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – Create a factory to pass items needed by the action (/src/App/Action/UserDbalListFactory.php)
  40. 57 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – Create the action (view 1 of 2 - constructor) (/src/App/Action/UserDbalListAction.php)
  41. 58 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – Create the action (view 2 of 2 - __invoke method) (/src/App/Action/UserDbalListAction.php cont’d)
  42. 59 Expressive Microservice Framework Blastoff • Database Connected Example With

    Doctrine – Create the view template (/templates/app/user-dbal-list.phtml)
  43. 61 Expressive Microservice Framework Blastoff • With Zend Expressive: –

    Easy to build middleware – Lightweight, add what is really needed – Fast – no extra load – Microservices in PHP are better
  44. 62 Expressive Microservice Framework Blastoff • Give Zend Expressive a

    Try...Today!!! – https://zendframework.github.io/zend-expressive/
  45. • Thank you! • Code at: https://github.com/adamculp/expressive-blastoff • Please rate

    at: https://joind.in/talk/142a9 Adam Culp http://www.rungeekradio.com http://www.geekyboy.com Twitter @adamculp Expressive Microservice Framework Blastoff