This article is outdated, it is a direct copy from medium
What is Zend Expressive, and how to use it
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
Zend Expressive is a PSR-7, 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 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!
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 implementation from The League of Extraordinary Packages — 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.
That’s being said, you can still use configuration; to do it just simply set a flag programmatic_pipeline to true
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.
In a middle of a middleware
In Zend Expressive 2.x you can use single-pass (a PSR-15 proposal with http-interop 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).
Double-pass is a little bit different as it passes more arguments to $next: a request, response and a callable
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!
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
You can check Zend Expressive official documentation here, and get Zend Expressive Skeleton here
Top comments (0)