HTTP Middleware is by no means a new concept, either in general or to PHP. The idea is to wrap your application logic (eg: controllers) up in a way that looks like an onion, having concentric layers of stuff happening before and after the central logic runs: reading from the request and writing to the response. Some layers might notice a problem and exit early, skipping your application logic altogether. Others will add headers to the response, or do other fancy stuff.
The issue with phalcon at the moment is that it doesn't provide a clear interface to assign them to specific routes. By using our previous package we already have this.
But in order for you to define your own Middleware you have to define them on its provider MiddlewareProvider
Global Middleware will run for every request while others will only run on defined route groups.
One thing to notice, this document won't cover how to implement a Middleware. We will point you to the phalcon docs, but we do follow a different usage of the returns for the Middleware. If the core wants to change the response type we just throw a proper response exception.
- The Middleware must implement its Interface
- On the call function you can either, return true to continue, false to halt the request, or throw an exception of the specific request you desire, based on our list of HTTP exceptions
- From the micro object, you can access services or any method Phalcon provides