<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mateusz Lopacinski</title>
    <description>The latest articles on DEV Community by Mateusz Lopacinski (@robopuff).</description>
    <link>https://dev.to/robopuff</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F100585%2F0fea1459-2486-49c5-a91e-63168ba9d1d2.png</url>
      <title>DEV Community: Mateusz Lopacinski</title>
      <link>https://dev.to/robopuff</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robopuff"/>
    <language>en</language>
    <item>
      <title>Introduction to Zend Expressive (1.x/2.x)</title>
      <dc:creator>Mateusz Lopacinski</dc:creator>
      <pubDate>Fri, 07 Apr 2017 12:03:54 +0000</pubDate>
      <link>https://dev.to/robopuff/introduction-to-zend-expressive-1-x-2-x-3m00</link>
      <guid>https://dev.to/robopuff/introduction-to-zend-expressive-1-x-2-x-3m00</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This article is outdated, it is a direct copy from medium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  What is Zend Expressive, and how to use it
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AxntErSQmfSj1OAbOviqS0g.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AxntErSQmfSj1OAbOviqS0g.jpeg"&gt;&lt;/a&gt;Image from &lt;a href="https://www.rawpixel.com/" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://www.rawpixel.com/" rel="noopener noreferrer"&gt;https://www.rawpixel.com/&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  TL;DR;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Zend Expressive&lt;/em&gt; is an easy to use, &lt;em&gt;PSR-15&lt;/em&gt;, &lt;em&gt;PSR-11&lt;/em&gt;, &lt;em&gt;PSR&lt;/em&gt;-7 compliant middleware atomic micro-framework that can work well as an entry point to Zend MVC, a smaller fully-featured web app or an API server.&lt;/p&gt;

&lt;p&gt;Go ahead and watch &lt;a href="https://www.youtube.com/watch?v=fhUG7JexO7U" rel="noopener noreferrer"&gt;&lt;em&gt;James Titcumb @ PHP Uk ’17&lt;/em&gt;: &lt;strong&gt;Kicking off with Zend Expressive and Doctrine ORM&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Zend Expressive&lt;/strong&gt; is a &lt;a href="https://github.com/php-fig/http-message" rel="noopener noreferrer"&gt;PSR-7&lt;/a&gt;, middleware-powered &lt;em&gt;micro&lt;/em&gt;-framework that is built around freedom of choice and can be an easy introduction to the world of &lt;em&gt;Zend Framework&lt;/em&gt; or it’s components. It does not lock you into it’s ecosystem; instead it gives you the ability to build applications using libraries that make you feel most comfortable to work with.&lt;br&gt;&lt;br&gt;
The skeleton installer asks you which &lt;em&gt;Router/DI/Template&lt;/em&gt; engine you’d like to use.&lt;/p&gt;
&lt;h2&gt;
  
  
  Freedom of choice
&lt;/h2&gt;

&lt;p&gt;You are bound to use one of the supported &lt;em&gt;routers&lt;/em&gt; and &lt;em&gt;templating engines&lt;/em&gt;, but implementation of &lt;em&gt;DI&lt;/em&gt; is open due to the fact that &lt;em&gt;Expressive&lt;/em&gt; uses &lt;a href="https://github.com/php-fig/container" rel="noopener noreferrer"&gt;PSR-11&lt;/a&gt; Containers.&lt;br&gt;&lt;br&gt;
Use of middleware and standardized &lt;em&gt;request/response&lt;/em&gt; also gives us the unique opportunity to mix different libraries easier than ever; just simply create a &lt;em&gt;callable&lt;/em&gt; for it, add it to &lt;em&gt;DI&lt;/em&gt; (if needed) and it’s ready!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;




&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Same ease of use is available if you’d like to add unsupported or custom written classes/libraries. If they require a configuration, just create a &lt;em&gt;factory&lt;/em&gt; and that’s all!&lt;/p&gt;

&lt;p&gt;Everything that is compatible with middlewares (and &lt;em&gt;PSR-15&lt;/em&gt;) will work in &lt;em&gt;Zend Expressive&lt;/em&gt;, and packagist is full of them — for example look for the &lt;a href="https://oauth2.thephpleague.com/" rel="noopener noreferrer"&gt;OAuth2&lt;/a&gt; implementation from &lt;a href="http://thephpleague.com/" rel="noopener noreferrer"&gt;&lt;em&gt;The League of Extraordinary Packages&lt;/em&gt;&lt;/a&gt; — it requires minimal work to be fully operational.&lt;/p&gt;

&lt;h2&gt;
  
  
  How about pipes
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Zend Expressive&lt;/em&gt; is all about _piping — _an atomizing, splitting your application into smaller chunks that poses (ideally) single responsibility.&lt;/p&gt;

&lt;p&gt;Documentation currently stands for &lt;em&gt;programmatic&lt;/em&gt; approach which is due to a fact that this is more flexible — bare in mind that routes are pipes as well.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;That’s being said, you can still use configuration; to do it just simply set a flag programmatic_pipeline to true&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;From now on you’ll be able to write routes in configuration again, but remember to set all required middleware in correct order or application might not work properly.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  In a middle of a middleware
&lt;/h2&gt;

&lt;p&gt;In &lt;em&gt;Zend Expressive 2.x&lt;/em&gt; you can use &lt;em&gt;single-pass&lt;/em&gt; (a &lt;a href="https://github.com/php-fig/fig-standards/tree/master/proposed/http-middleware" rel="noopener noreferrer"&gt;PSR-15&lt;/a&gt; proposal with &lt;a href="https://github.com/http-interop/http-middleware" rel="noopener noreferrer"&gt;http-interop&lt;/a&gt; implementation) or a &lt;em&gt;double-pass&lt;/em&gt; middleware.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Single-pass&lt;/em&gt; refers to implementation of Psr\Http\ServerMiddleware\MiddlewareInterface (or interop implementation Interop\Http\ServerMiddleware\MiddlewareInterface) which means that there is a method process which takes two parameters ServerRequestInterface $request and DelegateInterface $delegate but invoking it from a chain will require passing only the &lt;em&gt;request&lt;/em&gt; (interface is optional as the same can be achieved with __invoke(ServerRequestInterface $request, DelegateInterface $delegate) instead of the process method).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Double-pass&lt;/em&gt; is a little bit different as it passes more arguments to $next: a &lt;em&gt;request&lt;/em&gt;, &lt;em&gt;response&lt;/em&gt; and a &lt;em&gt;callable&lt;/em&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Both of them currently work but I believe that with the &lt;em&gt;PSR-15&lt;/em&gt; proposal becoming a standard, we will come to an end with &lt;em&gt;the double-pass&lt;/em&gt; approach as it will be fully replaced by a standardized version of it; which is a good thing — it could help us build more versatile libraries, that can be used everywhere, no matter which framework you are using (as long as it is standard compliant of course).&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases (in which ZE works best, but it’s not limited to)
&lt;/h2&gt;

&lt;p&gt;It is designed to make life easier, and it delivers. If you want to start a simple project — &lt;em&gt;Expressive&lt;/em&gt; is your best shot, as it’s out-of-the-box skeleton installer makes it pretty easy and still allows you to choose your preferred &lt;em&gt;DI&lt;/em&gt;, &lt;em&gt;Router&lt;/em&gt; and &lt;em&gt;Template engine&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;em&gt;PSR-15&lt;/em&gt;/&lt;em&gt;middleware&lt;/em&gt; approach comes an ease when creating API applications as it gives an opportunity to make it as a chain of responsibility — one step at a time, want an OAuth2 token checked and validated? Add middleware, simple as that!&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Zend Expressive&lt;/em&gt; shows us that usage of frameworks and their learning curve do not have to be a steep one, it does not have to be hard and at the same time it is not “magical” (looking at you, &lt;em&gt;Laravel&lt;/em&gt;) — it keeps things easy and standardized so if you learn it, you can use it with other projects that make use of &lt;em&gt;PSR-15,&lt;/em&gt; &lt;em&gt;PSR-11&lt;/em&gt;, &lt;em&gt;PSR-7&lt;/em&gt;. &lt;em&gt;Zend Framework&lt;/em&gt; was labelled of hard to get into, with not-so-easy to read documentation, and that was one of the reasons why &lt;em&gt;Expressive&lt;/em&gt; was created — as an entry point, to educate and promote &lt;em&gt;Zend MVC&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To summarize, &lt;em&gt;Zend Expressive&lt;/em&gt; will work best for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smaller projects or learning purposes — a perfect entry point to Zend MVC&lt;/li&gt;
&lt;li&gt;Projects that require modularity &lt;em&gt;and&lt;/em&gt;/&lt;em&gt;or&lt;/em&gt; freedom of choice in libraries&lt;/li&gt;
&lt;li&gt;Building an API&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Get started with Zend Expressive and be awesome right now!
&lt;/h2&gt;

&lt;p&gt;Just start a new project with &lt;em&gt;composer&lt;/em&gt; and &lt;em&gt;Zend Expressive Skeleton&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;composer create-project zendframework/zend-expressive-skeleton &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can check &lt;em&gt;Zend Expressive&lt;/em&gt; official documentation &lt;a href="https://docs.zendframework.com/zend-expressive/" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and get &lt;em&gt;Zend Expressive Skeleton&lt;/em&gt; &lt;a href="https://github.com/zendframework/zend-expressive-skeleton" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>expressive</category>
      <category>zend</category>
    </item>
  </channel>
</rss>
