PSR-7: HTTP Messages Today

PSR-7 is here and is big. Now, more than one month after it was voted, a lot of work has been put into projects supporting this standard. Even though we’re still at the beginning of this great journey, it is exciting to see what is already available, thanks to great work of PHP community.

Watching related projects since the inception of that standard, I will present packages that can serve as foundation for actual applications: HTTP message implementations, dispatchers and micro frameworks.

What is PSR-7?

If you don't know about PSR-7, now it's the right time to learn it! It defines a common standard for HTTP request and response representation in PHP. Why is it important? It's all about finding a common language between PHP libraries that do something with HTTP. Imagine being able to build your application using ANY libraries that are convenient to you: take authentication from Symfony 2, router from Aura, combine them with MVC from Zend Framework and all of them should play nicely with each other. You won't be locked with any full-stack solution, you can pick any component that works well for you.

If you are new to PSR-7, be sure to read this and this.

Official spec is available here.

PSR-7 implementations

Following libraries implement interfaces declared in psr/http-message (official PSR-7 package). They are replaceable - you can pick an implementation that is most handy for you and use it together with any PSR-7 framework.


Interesting name, isn't it? Took me a while to remember the spelling, but it provides great, complete implementation of PSR-7 messages.
Authored by the boss of Zend Framework - Matthew Weier O'Phinney. Initially developed as a proof-of-concept PSR-7 implementation, it evolved together with the standard to become full-featured, production-ready package.
Besides interface implementations, currently it supports serialization (creating messages from string and vice versa) and comes with handy classes for building HTTP responses.


Written by folks behind Guzzle, the most advanced HTTP client library written in PHP.
It's a PSR-7 library for building HTTP clients, at this moment it is missing ServerRequestInterface and UploadedFileInterface implementations (although PR implementing them are welcome by authors). It comes with interesting stream implementations: CachingStream for seeking on non-seekable streams, InflateStream for compressed data and so on. It also comes functional interface that allows (de)serialization, filtering, working with URIs and more.

PSR-7 Dispatchers

PSR-7 dispatcher is a library that can execute stack of middleware. In order to build your application on top of dispatcher, you'll need to add elements like dynamic router and IoC container yourself. This is great if you need flexibility, but if you are building something generic, you may want to start with microframework instead.


Explained as "a port of Sencha Connect ( to PHP", which doesn't say much about what it actually is.
Personally, I would define Stratigility as a middleware dispatcher combined with with static router. Still not clear? See what it can do:

  • it can work as a router for simple set of static pages:

    $app->pipe('/about-us', function (ServerRequestInterface $request, ResponseInterface $response) {
        $response->getBody()->write('<h1>About us</h1>');
        return $response;
    $app->pipe('/company', /* ... */);
    $app->pipe('/', /* ... */);
  • it can be used to funnel traffic between different PSR-7 compatible frameworks (or bridge middleware, if you want to connect with ZF1 for instance):

    $app->pipe('/user', $zf3Middleware);
    $app->pipe('/api', $apigilityMiddleware);
    $app->pipe('/home', $slimMiddleware);
    $app->pipe('/test', function (ServerRequestInterface $request, ResponseInterface $response) {
        $response->getBody()->write('Hello, world!');
        return $response;
  • finally, with help of zend-stratigility-dispatch (still under development), it can act as a micro framework (see skeleton app)


Another dispatcher, Relay, comes with interesting idea of "resolvers", allowing to dynamically instantiate middleware. This is simple but powerful - you can link it with your own Dependency Injection system, without using non-official container-interop standard.


Microframeworks here share the same base with dispatchers, adding libraries that you'll need for almost every web project now: router and (usually) IoC container.


Well known micro framework star, Slim is based on PSR-7 since version 3 (still in development). Compared with other projects explained here, Slim is a complex beast: supports sessions, HTTP cache, event dispatcher... it can do quite a lot for you before you'll need to install anything else.


Radar works as PSR-7 framework for applications designed according to ADR (Action-Domain-Responder) pattern defined by Paul M. Jones.
Based on middleware dispatcher, with Aura.Router for routing and Aura.Di for IoC container, Radar is a great base to experiment with ADR pattern.


Another new player, Harmony, attempts to create an "invisible framework", based on philosophy described in Phil Sturgeon's article. Harmony's author wants to create a minimal framework on top of low-weight, replaceable components like router and service container. At this moment it combines dispatcher with container-interop connector (container can be injected into middleware). Router is replaceable, defaults to FastRoute from Nikita Popov.
Harmony's idea is to start with just this elements and plug in anything else you need via composer.

PSR-7 support in full-stack frameworks

So far only Symfony and Laravel (as it is based on SF's HTTP) are directly supporting PSR-7 messages defined in standard ("directly" meaning that out of the box you can write controllers accepting ServerRequestInterface and returning ResponseInterface). PSR-7 support was added to current major versions, without a BC-break.
Zend Framework is going to support PSR-7 natively from version 3.0, although it is very likely that we will see a bridge, or at least middleware for zend-stratigility a bit earlier.


Last few weeks have been really exciting: imagine all those smart community gurus working together on common standard and then on code based on it. Future looks bright for PHP, I cannot wait to see how PSR-7 will be used in future projects!

Read more 

ZF-Console: PHP microframework for console applications

As you may know, folks from ZF2/Apigility projects keep bringing interesting utility modules, enriching ZF2 ecosystem. Today I discovered small framework/helper for writing console applications: ZF-Console. And, to my surprise, it was based on my own pull request from last year! I was really proud to see that I brought something useful to the community.

Read more 

Testing ZF2 module services

There's an important question often rising when working on Zend Framework 2 module: should I test service factories? After all, they are usually trivial, they create some object and inject it with dependencies from ServiceManager. Having one test per factory seems to be an overkill.

Better to go one step back, and ask yourself a question: what exactly do you want to test?

Read more 

MtMail: e-mail module for ZF2

I'm happy to present a ZF2 module that handles composing and sending e-mail messages.

Why another module? There are a few of them already available on ZF modules website. However, when I was looking for solution to use in my application, I quickly realized that most of them are either outdated, or they miss features I needed. That's why I decided to write my own.

My intention was to create something powerful, but still simple to use. You an customize e-mail headers, add layout, automatically generate plaintext version of HTML e-mail, and so on. But you can also start composing and sending e-emails from your controllers with just a few lines of code.

Read more 

Using standalone Zend\View

Zend\View is pretty advanced rendering engine, with multiple useful features. It is working nicely within ZF2's MVC stack, where it is automatically configured for you. But how to use it without full MVC?

This can be useful in some situations: when building Your Own Microframework™, when creating an application based on ZF2 components, or (in my case) when working on module that is supposed to render something outside MVC flow. All of this projects can benefit from nested templates, multiple rendering engines, or pluggable architecture of Zend\View.

So, how to do that?

Read more 

Extracting single table from huge MySQL dump

During last few weeks I had to work with relatively big MySQL dumps. I had to find interesting rows in about 400 files, each of them taking 40 minutes to import. In order to speed things up, I found simple tool that allowed me to extract only interesting tables.

The tool is actually single Perl script, named (available on Github). It allows extracting tables with simple command:

mat@server:~$ -t TABLE_NAME -r DUMP_FILE.sql

This command will print dump to console output, so you may want to redirect it to some file:

mat@server:~$ -t TABLE_NAME -r DUMP_FILE.sql > table_name.sql

Finally, is able to read input from stdin, so it is easy to extract and import single table from compressed dump file:

mat@server:~$ zcat DUMP_FILE.sql | -t TABLE_NAME \
| mysql dest_database -u username -p
Read more 

About me
Mateusz Tymek

After compiling my very first lines of code at the age of 12, I became passionate about computer science and technology. Now I'm a PHP developer, enjoying my work as a member of Cleeng team.
Doing some sports in my spare time.

Github activity