With PHP frameworks being more decoupled than ever, and with the help of a package and dependency manager, large and heavy PHP frameworks are becoming a thing of the past. Modern PHP developers now have a wealth of libraries available that specialize at specific tasks, and microservices are fast becoming a preferred way to architect applications. But many don't know how to start.
This talk will briefly introduce what microservices are, and how to use them. Then show how to build a foundation using the Zend Expressive microframework leveraging components of Zend Framework, and other libraries, to quickly create awesome things without requiring an entire framework. Resources for reference and continued learning will also be shared.
2. 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. 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!!!
8. 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. 9
Expressive Microservice Framework Blastoff
●
But in PHP...
– How to keep microservices light?
– Microservices shouldn’t be heavy
I’m a
Microservice!!!
10. 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. 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)
13. 13
Expressive Microservice Framework Blastoff
●
All The Things!!!
– So many tools:
Monolog
Whoops
Flysystem
IBMiToolkit
OAuth2 Server
https://github.com/ziadoz/awesome-php
Expressive
15. 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. 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. 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. 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. 19
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Done right from the start
All code available at: https://github.com/adamculp/expressive-blastoff
21. 21
Expressive Microservice Framework Blastoff
●
Composer Install Script
– Minimal skeleton or full?
●
With or without samples
●
Flat or with modules!
41. 41
Expressive Microservice Framework Blastoff
●
Header Middleware
– The class to include the very important header
(/src/App/src/Middleware/TheClacksMiddleware.php)
42. 42
Expressive Microservice Framework Blastoff
●
Header Middleware
– Add the middleware to the pipeline
– It will always be included
(/config/pipeline.php)
45. 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. 46
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Specify adapter (via Zend/Db/ConfigProvider() in this case)
(/config/config.php)
47. 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. 48
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Add the new action to dependencies
(/config/autoload/routes.global.php)
50. 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. 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. 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. 53
Expressive Microservice Framework Blastoff
●
Database Connected Example With Zend Db
– Create the view template
(/templates/app/user-list.phtml)
55. 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