Learn the concepts of PSR-7 middleware with Zend Expressive and how your application could be developed from scratch adapting those concepts with a new mindset. You'll see the different approaches, advantages and disadvantages, and the contrast of this paradigm and other more conventional paradigms.
3. Christian Varela
• I have a wife and 3 daughters
• I am from Mexico
• Master Degree in Computer Science
• 13 years programming with PHP
• I live in Miami
• I created Conqueror Soft Inc
• I play guitar and piano
3
5. Conqueror Soft will take your business to the next Level!
5
www.conquerorsoft.com
info@conquerorsoft.com
facebook.com/conquerorsoft
6. What is this session about?
Learn the concepts of PSR-7 middleware with Zend Expressive and how
your application could be developed from scratch adapting those
concepts with a new mindset. You'll see the different approaches,
advantages and disadvantages, and the contrast of this paradigm and
other more conventional paradigms.
6
7. Expressive
Expressive it is a framework by Zend that allows you to
write PSR-7 middleware applications for the web.
7
8. PSR-7
1. PHP Standard Recommendation 7
2. HTTP message interfaces
3. Describes common interfaces for representing HTTP messages as
described in RFC 7230 and RFC 7231
4. Describes interfaces for representing URIs for use with HTTP messages
as described in RFC 3986.
8
9. HTTP messages
1. They are messages between servers and clients in the web
1. HTTP request messages
2. HTTP response messages
9
10. HTTP Request messages
1. Request line
1. Method
2. Target
3. HTTP protocol version
2. One or more headers
3. empty line
4. Message body
10
12. HTTP Response messages
1. Status line
1. HTTP protocol version Method
2. HTTP status code
3. Reason phrase
2. One or more headers
3. empty line
4. Message body
12
16. Middleware
Middleware is code that exists between the request and response, and
which can take the incoming request, perform actions based on it, and either
complete the response or pass delegation on to the next middleware in the
queue.
16
17. Middleware
For example, you could have a collection of middleware like this:
1.reverse string
2.convert string to upper case
3.shuffle string
17
26. Expressive 2 default middlewares
1. Route middleware: parses and identifies the middleware to handle the
request
2. Dispatching middleware: call the middleware identified by route
26
27. Expressive implementations
1. psr/http-message (Common interface for HTTP messages) (based on
PSR-7)
1. zend-stratigility (Middleware for PHP)
2. zend-diactoros (PSR HTTP Message implementations)
3. http-interop/http-middleware (Common interface for HTTP server-side
middleware)
27
32. Error handler supported
1. Whoops: whoops is an error handler framework for PHP. Out-of-the-box,
it provides a pretty error interface that helps you debug your web
projects, but at heart it's a simple yet powerful stacked error handling
system.
32
33. Zend-Diactoros
zend-diactoros is a PHP package containing implementations of
the accepted PSR-7 HTTP message interfaces, as well as a "server"
implementation similar to node's http.Server.
All the responses will be handled with Diactoros.
33
34. Interop 0.4.1
1 <?php
2 namespace InteropHttpServerMiddleware;
3
4 use PsrHttpMessageResponseInterface;
5 use PsrHttpMessageServerRequestInterface;
6
7 interface MiddlewareInterface
8 {
9 public function process(
10 ServerRequestInterface $request,
11 DelegateInterface $delegate
12 ) : ResponseInterface;
13 }
14
15 interface DelegateInterface
16 {
17 public function process(
18 ServerRequestInterface $request
19 ) : ResponseInterface;
20 }
34
36. Web application
1. It will consume a remote API for authors to be able to do:
1. List of authors
2. Adding authors
3. Editing authors
4. Deleting authors
36
40. Choose application type
What type of installation would you like?
[1] Minimal (no default middleware, templates, or assets; configuration
only)
[2] Flat (flat source code structure; default selection)
[3] Modular (modular source code structure; recommended)
Make your selection (2): 3
40
41. Choose Container for DI
Which container do you want to use for dependency injection?
[1] Aura.Di
[2] Pimple
[3] Zend ServiceManager
Make your selection or type a composer package name and version
(Zend ServiceManager):
41
42. Choose Router
Which router do you want to use?
[1] Aura.Router
[2] FastRoute
[3] Zend Router
Make your selection or type a composer package name and version
(FastRoute):
42
43. Choose Template engine
Which template engine do you want to use?
[1] Plates
[2] Twig
[3] Zend View installs Zend ServiceManager
[n] None of the above
Make your selection or type a composer package name and version (n):1
43
44. Choose Error handler
Which error handler do you want to use during development?
[1] Whoops
[n] None of the above
Make your selection or type a composer package name and version
(Whoops):
44
47. Create Authors’ list
1. Configure templates in ConfigProvider
2. Create Authors list action class (ListAction.php)
3. Register class in dependencies in configuration.
4. Configure routes in routes.php
5. Generate factory for ListAction
6. Update the dependencies in dependencies.global.php
47
48. Create Authors’ list
7. Configure global.php file
8. composer require zendframework/zend-http
9. Configure local.php with credentials
10.composer require zendframework/zend-json
11.Modify list template
12.create AuthorRestCollection
13.php -S 0.0.0.0:8082 -t public
48
59. Multiple routes in single class
1. Create Abstract page class in App
2. Create AuthorsPage class in Authors/Action
3. Create AuthorsPageFactory class
4. Create indexAction in AuthorsPage
5. Create authors-index.phtml template
6. Configure factories in dependencies.global.php
7. Update routes configuration
59
60. Multiple routes in single class
8. Test index action
9. Create form
10.Add addAction
11.Create add template
12.Add saveAuthor method to AuthorRestCollection
60
69. Editing authors
1. Add edit action
2. Add edit template
3. Create getAuthor in RestCollection
4. Change the condition to be 200 or 201 depending on the presence of
entity_id
69
70. Editing authors
5. Modify factories in order to pass settings later
6. Configure editAction
7. Configure edit template
70
73. AuthorRestCollection saveAuthor
1 public function saveAuthor(Author $author)
2 {
3 ...
4 if(empty($entity_id) and $res->getStatusCode()!=201)
5 {
6 throw new RuntimeException("The author could not be saved.");
7 }
8 if(!empty($entity_id) and $res->getStatusCode()!=200)
9 {
10 throw new RuntimeException("The author could not be saved.");
11 }
12 }
73
74. AuthorRestCollection getAuthor
1 public function getAuthor($id)
2 {
3 $id=(int)$id;
4 $this->httpClient->setMethod('GET');
5 $this->httpClient->setUri($this->httpClientUri."/$id");
6 $res=$this->httpClient->send();
7 if($res->getStatusCode()!=200)
8 {
9 throw new RuntimeException(sprintf("There is no author
with ID %d", $id));
10 }
11 $json=$this->getJson();
12 $all=$json->decode($res->getContent());
13 $author=new Author();
14 $author->exchangeArray(get_object_vars($all));
15 return $author;
16 }
74
82. Middleware advantages
1. Easy to learn
2. Easy to implement
3. You don’t loose modularity if you are used to MVC
4. Flexibility, you can use other libraries for routing, templating, DI, etc.
5. It adapts to small and big projects.
6. Easy to implement Zend-* libraries if you are used to them
82
83. Middleware disadvantages
1. It’s a relatively “new” conceptual implementation in PHP, which is subject
to more changes or adjustments and you’ll need to be ready if you want
to catch up with updates
2. since it’s relatively “new” also, there are no too many people with
experience on this.
83