Slide 1

Slide 1 text

Expressive Microservice Framework Blastoff By: Adam Culp Twitter: @adamculp https://joind.in/talk/142a9

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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!!!

Slide 4

Slide 4 text

4 Expressive Microservice Framework Blastoff ● I Help Build The Web

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

7 Expressive Microservice Framework Blastoff ● Buzzword Bingo – We will mention many buzzwords, but...

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

9 Expressive Microservice Framework Blastoff ● But in PHP... – How to keep microservices light? – Microservices shouldn’t be heavy I’m a Microservice!!!

Slide 10

Slide 10 text

10 Expressive Microservice Framework Blastoff ● Full Stack Frameworks Suck – Heavy and bloated – “Kitchen Sink” – “You don’t have to use everything, but its there...”

Slide 11

Slide 11 text

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)

Slide 12

Slide 12 text

12 Expressive Microservice Framework Blastoff ● PHP Ecosystem Facilitators – PHP 7 – Microframeworks – Libraries – Components – Containers – Composer

Slide 13

Slide 13 text

13 Expressive Microservice Framework Blastoff ● All The Things!!! – So many tools: Monolog Whoops Flysystem IBMiToolkit OAuth2 Server https://github.com/ziadoz/awesome-php

Slide 14

Slide 14 text

14 Expressive Microservice Framework Blastoff ● Communication Sucks – Say what!?! Monolog Whoops Flysystem IBMiToolkit OAuth2 Server

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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/

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

19 Expressive Microservice Framework Blastoff ● Composer Install Script

Slide 20

Slide 20 text

20 Expressive Microservice Framework Blastoff ● Composer Install Script – Minimal skeleton or full*? ● With or without samples

Slide 21

Slide 21 text

21 Expressive Microservice Framework Blastoff ● Composer Install Script – Router options ● Aura.Router ● FastRoute* ● Zend Router

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

23 Expressive Microservice Framework Blastoff ● Composer Install Script – Template engine options: ● Plates ● Twig ● Zend View ● None*

Slide 24

Slide 24 text

24 Expressive Microservice Framework Blastoff ● Composer Install Script – Error handler options: ● Whoops* ● None

Slide 25

Slide 25 text

25 Expressive Microservice Framework Blastoff ● Structure – Folder structure of Zend Expressive Skeleton

Slide 26

Slide 26 text

26 Expressive Microservice Framework Blastoff ● Skeleton Application

Slide 27

Slide 27 text

27 Expressive Microservice Framework Blastoff ● Not MVC – Files and structure focused on Actions

Slide 28

Slide 28 text

28 Expressive Microservice Framework Blastoff ● The Flow

Slide 29

Slide 29 text

29 Expressive Microservice Framework Blastoff ● Initial File – Front controller (/public/index.php)

Slide 30

Slide 30 text

30 Expressive Microservice Framework Blastoff ● Container Creation – We specified Zend ServiceManager (/config/container.php)

Slide 31

Slide 31 text

31 Expressive Microservice Framework Blastoff ● Load Configs (/config/config.php)

Slide 32

Slide 32 text

32 Expressive Microservice Framework Blastoff ● Load Dependencies – Items to be called as middleware in routes. (/config/autoload/routes.global.php)

Slide 33

Slide 33 text

33 Expressive Microservice Framework Blastoff ● Load Routes – Matches path to middleware (Dependencies shown earlier) (/config/autoload/routes.global.php cont’d)

Slide 34

Slide 34 text

34 Expressive Microservice Framework Blastoff ● Action Anatomy – Creates raw JSON response (/src/App/Action/PingAction.php)

Slide 35

Slide 35 text

35 Expressive Microservice Framework Blastoff ● JSON Response – Created by action

Slide 36

Slide 36 text

36 Expressive Microservice Framework Blastoff ● Let’s Create a Middleware!!!

Slide 37

Slide 37 text

37 Expressive Microservice Framework Blastoff ● Header Middleware – Create class for new middleware (or include someone else’s)

Slide 38

Slide 38 text

38 Expressive Microservice Framework Blastoff ● Header Middleware – The class to include the very important header (/src/App/Middleware/TheClacksMiddleware.php)

Slide 39

Slide 39 text

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)

Slide 40

Slide 40 text

40 Expressive Microservice Framework Blastoff ● Header Middleware – Header for every call now carries our important message

Slide 41

Slide 41 text

41 Expressive Microservice Framework Blastoff ● Let’s Create a Database Connected Middleware!!! ● With Zend Db

Slide 42

Slide 42 text

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!

Slide 43

Slide 43 text

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)

Slide 44

Slide 44 text

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)

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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)

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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)

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

51 Expressive Microservice Framework Blastoff ● Database Connected Example With Zend Db – Rejoice!

Slide 52

Slide 52 text

52 Expressive Microservice Framework Blastoff ● Let’s Create a Database Connected Middleware!!! ● With Doctrine!!!

Slide 53

Slide 53 text

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!

Slide 54

Slide 54 text

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)

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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)

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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)

Slide 59

Slide 59 text

59 Expressive Microservice Framework Blastoff ● Database Connected Example With Doctrine – Create the view template (/templates/app/user-dbal-list.phtml)

Slide 60

Slide 60 text

60 Expressive Microservice Framework Blastoff ● Database Connected Example With Doctrine – Rejoice!

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

62 Expressive Microservice Framework Blastoff ● Give Zend Expressive a Try...Today!!! – https://zendframework.github.io/zend-expressive/

Slide 63

Slide 63 text

● 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