PHP-Middleworld is Out!
Collecting PHP PSR-7 compliant Middleware
We just released php-middleworld.com, a repository for PSR-7 compliant middleware in PHP, and we would like to share our progress and what the future may bring.
PSR-7
It all started with PSR-7, a set of common interfaces for representing HTTP messages in PHP, allowing application abstraction from the HTTP layer.
The most important aspect of PSR-7 is that it is framework independent and hence its interfaces can easily be used with multiple frameworks such as Symfony, Zend Framework, Laravel or any other framework.
This capability allows developers to write framework agnostic code with a wider opportunity to be reused in other projects, relying on the PSR-7 interfaces only.
PHP AND MIDDLEWARE
The other important key-word is middleware: once the PHP ecosystem gained a nice abstraction over HTTP messages, it was time to think about the best way to write web applications on top of this abstraction.
Citing Matthew Weier O’Phinney, the concept of middleware can be summarized in a single method signature:
1 |
function (request, response) : response |
The idea is that objects representing the HTTP request and the HTTP response are passed to a callable, which does something with them to return an HTTP response.
This turns out to be a very easy and composable pattern, which had actually already been used with great success in several other programming languages.
The standard way to compose such middleware is to pass a third argument to the callable, so to allow for another layer of middleware to be invoked:
1 2 3 4 5 6 7 8 9 10 |
function (request, response, next) : response { // do something on request response = next(request, response) // do something on response return response } |
Several frameworks, as Zend Expressive, Slim, Radar or Spiral rely nowadays upon the concept of middleware to create web applications.
MIDDLEWARE REUSE
Now that we have a highly composable pattern based upon a framework agnostic HTTP abstraction, we can actually start writing highly reusable components that would work on any PHP project. This definitely helps developers easing their daily job.
We can have middleware taking care of logging all received requests, middleware to perform routing, middleware to manage authentication and some other to take care of user sessions. We use our framework to simply glue all this pieces together in the appropriate way.
The key question at this point should be: has anybody else already implemented the piece of middleware I need for the application I’m developing?
PHP-MIDDLEWORLD IS HERE!
Until now the answer to the previous question could only be gathered through Packagist, Github, or – in the worst case – google.
Having received so much from the community over the years, we thought it was about time to give something back. That’s how php-middleworld.com was born. With the aim to provide fellow PHP developers a means to quickly locate the middleware they need (often with multiple implementation choices).
THE FUTURE
We already have in mind several improvements for php-middleworld.com.
First of all we would like to enable fellow developers to add their own middleware, so that our database could grow with a shared effort.
Then we would also like to add PSR-15 compatible middleware support also, so to be ready when that proposal will be approved.
No need to get any deeper here, to find out more just head to php-middleworld.com and follow @phpmwd on Twitter!
Stay tuned!