HTTP based middlewares


In general the concept is to model your application around an HTTP request/response. That means the programming language has in some way an standardised HTTP request and response which can be used by each middleware. If an request arrives at the application it will call the first middleware which can then modify the response and call the next middleware in the stack and so on. An application stack of middlewares could look like this:

  • AuthenticationMiddleware
  • LogMiddleware
  • CachingMiddleware
  • Application

I.e. The AuthenticationMiddleware could inspect the Authorization header or the LogMiddleware could create an log entry for each request. In this way you could easily extend the behaviour of your application by simply adding new middlewares. Infact this concept is not new and many languages have developed an standard. To give you some examples:

PHP world

In the PHP world we currently not have such an standard. But in order to create a middleware we first need an standardised HTTP request and response. Currently almost every major framework has its own implementation of an HTTP request/response. The most widespread is probably the request and response from Symfony. But there is no framework independent standard which describes an HTTP message.

Fortunately there is PSR-7 under development which tries to describe such an standard. Currently the PSR is not completed but if its done it could increase the interoperability between projects. In the end this could lead to an middleware standard based on PSR-7.

PSX middlewares

With the 0.9.4 release of PSX I have introduced a new feature which gives you the possibility to define middlewares for your application. To explain this more in detail.

The controller now implements a new interface ApplicationStackInterface. This interface has only a single method which returns an array of middlewares. By default the application stack returns the ControllerExecutor middleware which calls the method of the controller. But it would also be possible to use other middlewares. I.e. the most basic example to simply write "Hello world" as response could be:

class FooController extends ControllerAbstract
    public function getApplicationStack()
        return [
            function($request, $response){
                $response->getBody()->write('Hello world');


Overwriting the application stack in the controller is optional but if you need to handle i.e. authentication or other things on the HTTP level this would be a nice way to handle this. I have already implemented some useful middlewares which handle i.e. Authentication, Caching or CookieEncryption. Take a look at the PSX\Dispatch\Filter namespace to see all available middlewares.


Since PSX is an API framework which mostly works on the HTTP level Iam highly interested in such an standard. The problem with the current PSR standard is that it models the HTTP request/response as value objects which are immutable and PSX works currently with mutable HTTP request and response objects.

Because of that I dont plan to implement the PSR interfaces directly into PSX but I will provide factory classes with them it is possible to create an PSR request/response from an PSX request/response and vice versa. So at least it would be possible to call PSR-enabled libraries from within PSX. Nevertheless Iam looking really forward to this PSR and since it is not done yet it could also probably go the mutable way.