Introduction to Zend Expressive (1.x/2.x)
Tags: php, expressive, zend
Published: 2019-04-03 10:46:08

---
title: Introduction to Zend Expressive (1.x/2.x)
published: true
tags: php,expressive,zend
canonical_url: https://medium.com/@robopuff/introduction-to-zend-expressive-ce9e3db64a4e
---

```
This article is outdated, it is a direct copy from medium
```

# What is Zend Expressive, and how to use it

![](https://cdn-images-1.medium.com/max/1024/1*xntErSQmfSj1OAbOviqS0g.jpeg)<figcaption>Image from <a href="https://www.rawpixel.com/">https://www.rawpixel.com/</a></figcaption>

## TL;DR;

_Zend Expressive_ is an easy to use, _PSR-15_, _PSR-11_, _PSR_-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.

Go ahead and watch [_James Titcumb @ PHP Uk ’17_: **Kicking off with Zend Expressive and Doctrine ORM**](https://www.youtube.com/watch?v=fhUG7JexO7U)

**Zend Expressive** is a [PSR-7](https://github.com/php-fig/http-message), middleware-powered _micro_-framework that is built around freedom of choice and can be an easy introduction to the world of _Zend Framework_ 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.  
The skeleton installer asks you which _Router/DI/Template_ engine you’d like to use.

## Freedom of choice

You are bound to use one of the supported _routers_ and _templating engines_, but implementation of _DI_ is open due to the fact that _Expressive_ uses [PSR-11](https://github.com/php-fig/container) Containers.  
Use of middleware and standardized _request/response_ also gives us the unique opportunity to mix different libraries easier than ever; just simply create a _callable_ for it, add it to _DI_ (if needed) and it’s ready!

{% gist https://gist.github.com/robopuff/caedc94d203aeb7a1283bfabfd18be89 %}

{% gist https://gist.github.com/robopuff/921f3ff88f3de84354d98976fbf2252e %}

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 _factory_ and that’s all!

Everything that is compatible with middlewares (and _PSR-15_) will work in _Zend Expressive_, and packagist is full of them — for example look for the [OAuth2](https://oauth2.thephpleague.com/) implementation from [_The League of Extraordinary Packages_](http://thephpleague.com/) — it requires minimal work to be fully operational.

## How about pipes

_Zend Expressive_ is all about _piping — _an atomizing, splitting your application into smaller chunks that poses (ideally) single responsibility.

Documentation currently stands for _programmatic_ approach which is due to a fact that this is more flexible — bare in mind that routes are pipes as well.

{% gist https://gist.github.com/robopuff/3db45dec553d104d9105333933339ec2 %}

That’s being said, you can still use configuration; to do it just simply set a flag programmatic\_pipeline to true

{% gist https://gist.github.com/robopuff/6cf333710445a846db8fd11acd92ea1c %}

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.

{% gist https://gist.github.com/robopuff/389fedef286cc73bf40502755b58fa84 %}

## In a middle of a middleware

In _Zend Expressive 2.x_ you can use _single-pass_ (a [PSR-15](https://github.com/php-fig/fig-standards/tree/master/proposed/http-middleware) proposal with [http-interop](https://github.com/http-interop/http-middleware) implementation) or a _double-pass_ middleware.

_Single-pass_ 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 _request_ (interface is optional as the same can be achieved with \_\_invoke(ServerRequestInterface $request, DelegateInterface $delegate) instead of the process method).

{% gist https://gist.github.com/robopuff/629d4526773d9fc79308476dda6a7c21 %}

_Double-pass_ is a little bit different as it passes more arguments to $next: a _request_, _response_ and a _callable_

{% gist https://gist.github.com/robopuff/a230db2e8c8a201ea945690fb55aa854 %}

Both of them currently work but I believe that with the _PSR-15_ proposal becoming a standard, we will come to an end with _the double-pass_ 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).

## Use cases (in which ZE works best, but it’s not limited to)

It is designed to make life easier, and it delivers. If you want to start a simple project — _Expressive_ 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 _DI_, _Router_ and _Template engine_.

With _PSR-15_/_middleware_ 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!

{% gist https://gist.github.com/robopuff/1309f64fc9eb016c2a5f33f41e4bb30c %}

_Zend Expressive_ 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, _Laravel_) — it keeps things easy and standardized so if you learn it, you can use it with other projects that make use of _PSR-15,_ _PSR-11_, _PSR-7_. _Zend Framework_ was labelled of hard to get into, with not-so-easy to read documentation, and that was one of the reasons why _Expressive_ was created — as an entry point, to educate and promote _Zend MVC_.

To summarize, _Zend Expressive_ will work best for:

- Smaller projects or learning purposes — a perfect entry point to Zend MVC
- Projects that require modularity _and_/_or_ freedom of choice in libraries
- Building an API

## Get started with Zend Expressive and be awesome right now!

Just start a new project with _composer_ and _Zend Expressive Skeleton_:

_composer create-project zendframework/zend-expressive-skeleton <project-path>_

You can check _Zend Expressive_ official documentation [here](https://docs.zendframework.com/zend-expressive/), and get _Zend Expressive Skeleton_ [here](https://github.com/zendframework/zend-expressive-skeleton)