Expressive Microservice
Framework Blastoff
By:
Adam Culp
Twitter: @adamculp
https://joind.in/talk/cd0e6
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
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
I am the
PHP Ninja!!!
4
Expressive Microservice Framework Blastoff
●
I Help Build The Web
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
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
7
Expressive Microservice Framework Blastoff
●
Buzzword Bingo
– We will mention many buzzwords, but...
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
9
Expressive Microservice Framework Blastoff
●
But in PHP...
– How to keep microservices light?
– Microservices shouldn’t be heavy
I’m a
Microservice!!!
10
Expressive Microservice Framework Blastoff
●
Full Stack Frameworks Suck
– Heavy, bloated, slow
– “Lock” you in
– “Kitchen Sink”
– “You don’t need everything, but its there...”
11
Expressive Microservice Framework Blastoff
●
Need For Speed
– What does a microservice “need”?
●
HTTP message layer
●
Routing capabilities
●
Dependency injection
– IoC
– Testable
– Swappable pieces
●
Templating
– Optional (APIs may not need it, except documentation)
12
Expressive Microservice Framework Blastoff
●
PHP Ecosystem Facilitators
– Composer
– Components
– Libraries
– Containers
– Microframeworks
– PHP 7
13
Expressive Microservice Framework Blastoff
●
All The Things!!!
– So many tools:
Monolog
Whoops
Flysystem
IBMiToolkit
OAuth2 Server
https://github.com/ziadoz/awesome-php
Expressive
14
Expressive
Expressive Microservice Framework Blastoff
●
Communication Sucks
– Say what!?!
Monolog
Whoops
Flysystem
IBMiToolkit
OAuth2 Server
15
Expressive Microservice Framework Blastoff
●
PSR-15 (preferred)
– Part of PHP-Fig.org recommendations
– Must implement
●
PsrHttpServerMiddlewareMiddlewareInterface
●
Middleware dispatching systems must implement
– PsrHttpServerMiddlewareDelegateInterface
– PSR-7 Double Pass
●
PsrHttpMessageMessageInterface
– PsrHttpMessageRequestInterface
●
PsrHttpMessageServerRequestInterface
– PsrHttpMessageResponseInterface
●
PsrHttpMessageStreamInterface
● PsrHttpMessageUploadFileInterface
●
PsrHttpMessageUriInterface
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
17
Expressive Microservice Framework Blastoff
●
Zend Expressive
– Microframework built around middleware
– Versions
● Version 1 based on PSR-7
●
Version 2 based on PSR-15
– Very lean runtime
– Use for building:
●
APIs
●
Web applications
● Single page sites
– Choose your own stack
– Great documentation
●
https://zendframework.github.io/zend-expressive/
18
Expressive Microservice Framework Blastoff
●
Zend Expressive
– Not built on Zend Framework, but build by ZF team
●
Zend Framework 3 does include some components now
●
You choose the stack
19
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Done right from the start
All code available at: https://github.com/adamculp/expressive-blastoff
20
Expressive Microservice Framework Blastoff
●
Composer Install Script
21
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Minimal skeleton or full?
●
With or without samples
●
Flat or with modules!
22
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Container options: (container interop)
●
Aura.Di
●
Pimple
●
Zend ServiceManager*
23
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Router options
●
Aura.Router
●
FastRoute*
●
Zend Router
24
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Template engine options:
●
Plates
●
Twig
●
Zend View
●
None*
25
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Error handler options:
●
Whoops*
●
None
26
Expressive Microservice Framework Blastoff
●
Structure
– Folder structure of Zend Expressive Skeleton
27
Expressive Microservice Framework Blastoff
●
Skeleton Application
28
Expressive Microservice Framework Blastoff
●
Not MVC
– Files and structure focused on Actions
29
Expressive Microservice Framework Blastoff
●
The Flow
30
Expressive Microservice Framework Blastoff
●
Initial File
– Front controller (/public/index.php)
31
Expressive Microservice Framework Blastoff
●
Container Creation
– We specified Zend ServiceManager
(/config/container.php)
32
Expressive Microservice Framework Blastoff
●
Load Configs
(/config/config.php)
33
Expressive Microservice Framework Blastoff
●
Load Configs
(/src/App/src/ConfigProvider.php)
34
Expressive Microservice Framework Blastoff
●
Load More Configs
35
Expressive Microservice Framework Blastoff
●
Load Pipelines
– Items to be called as the middleware pipeline.
(/config/pipeline.php)
36
Expressive Microservice Framework Blastoff
●
Load Pipelines
– Items to be called as routed middleware.
(/config/routes.php)
37
Expressive Microservice Framework Blastoff
●
Action Anatomy
– Creates raw JSON response
(/src/App/src/Action/PingAction.php)
38
Expressive Microservice Framework Blastoff
●
JSON Response
– Created by action
39
Expressive Microservice Framework Blastoff
●
Let’s Create a Middleware!!!
40
Expressive Microservice Framework Blastoff
●
Header Middleware
– Create class for new middleware (or include someone else’s)
41
Expressive Microservice Framework Blastoff
●
Header Middleware
– The class to include the very important header
(/src/App/src/Middleware/TheClacksMiddleware.php)
42
Expressive Microservice Framework Blastoff
●
Header Middleware
– Add the middleware to the pipeline
– It will always be included
(/config/pipeline.php)
43
Expressive Microservice Framework Blastoff
●
Header Middleware
– Header for every call now carries our important message
44
Expressive Microservice Framework Blastoff
●
Let’s Create a Database Connected Middleware!!!
●
With Zend Db
45
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!
46
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Specify adapter (via Zend/Db/ConfigProvider() in this case)
(/config/config.php)
47
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)
48
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Add the new action to dependencies
(/config/autoload/routes.global.php)
49
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Add the new route to dependencies
(/config/routes.php
50
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Create a factory to pass items needed by the action
(/src/App/src/Action/UserListFactory.php)
51
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Create the action (view 1 of 2 - constructor)
(/src/App/src/Action/UserListAction.php)
52
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Create the action (view 2 of 2 - __invoke method)
(/src/App/src/Action/UserListAction.php cont’d)
53
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Create the view template
(/templates/app/user-list.phtml)
54
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Rejoice!
64
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
65
Expressive Microservice Framework Blastoff
●
Give Zend Expressive a Try...Today!!!
– https://zendframework.github.io/zend-expressive/
●
Thank you!
●
Code at: https://github.com/adamculp/expressive-blastoff
●
Please rate at: https://joind.in/talk/cd0e6
Adam Culp
http://www.rungeekradio.com
http://www.geekyboy.com
Twitter @adamculp
Expressive Microservice Framework Blastoff

Expressive Microservice Framework Blastoff