Middleware order of execution


#1

Hello boys (and girls),

I have a small issue with Slim. I’m building this huge rest api and I have few middleware components that I need to be run in certain order…

The problem is that some are global (all routes) some are route specific…

I have 3 components: HeaderParametersAuthentication, UserCredentialsAuthentication and TokenValidation.

Only in the case of “token/request” route you don’t need TokenValidation Middleware. For every other resource you have to have all 3 so I put the first 2 on “global” add() and token validation on routes. My problem is that the checks are executed in “token validation”, “user credentials auth” and then HeaderParameters order which cause my APP to first check for request data, then checks if user has credentials and at the very and it checks headers which should be the first thing that is checked. How can I change the order of execution to match my needs?Is that even possible in slim?

I need HeaderParametersAuth to be first thing to check because if users sends wrong content-type (ie. application/jsn (TYPO!)) the order of execution will cause the app to behave very strange because at this point headers are checked at the very end. I tried moving middleware around in the chain but to no avail…

tia!


#2

got it! In my case solution was to add check for errors (of previous middewares) in every middleware…