PSR standards · Question 4 of 12
What may a PSR-15 middleware do with an incoming request?
3 answers are correct. Easy
Show answer
Answer
- B Modify the response returned by the next handler before returning it
- C Create and return its own response without calling the next handler
- E Modify or enrich the request and then pass it on to the next handler
Explanation
A middleware wraps the actual application like a layer of an onion: it receives the request, can enrich it (e.g. with withAttribute()) and passes it on with $handler->handle($request). It can also short-circuit the chain and return its own response directly, such as a redirect or a 403 response when access is denied. On the way back it can still adjust the response, for example by adding an extra header. Sending output via echo contradicts the concept, because every middleware must return a ResponseInterface.