Zend MVC Middleware on every request

zend-mvc

#1

I’m currently using ZF3 with a large code base and I want to start writing middleware. As of Zend/mvc 3.1 you can create an array of middleware to be piped for a given route/path in the configuration; great feature. See the following link for some reference; https://www.jamestitcumb.com/posts/middleware-in-zend-mvc. However, I have not seen the capability to specify a set of middleware to be run on every request (ex. ip filter, CORS, auth, logging etc.) like you can in Zend Expressive. One can accomplish this in Zend Expressive by piping middleware before the routing middleware; makes sense. Does this capability exist in ZF3?


#2

No, this feature does not exist. It can be done within event listeners by having the listener pull the request from the event, pass it to the psr7bridge to convert it, and then dispatch your middleware or pipeline with it. If you do, I’d suggest passing the MvcEvent as a request attribute.


#3

Do you know if there have been any updates to ZF 3 that would allow me to implement what I previously suggested? You did give me an alternative option about a year ago, which I have contemplated, but I was just wondering if this functionality has been added yet.


#4

Nope, not yet. Feel free to send a pull request if you’re interested, though!