Architecture and request · Question 10 of 12
A middleware calls $request->withAttribute('myext.region', $region) and then return $handler->handle($request). The attribute is missing in the controller. What is the mistake?
1 answer is correct. Medium
Show answer
Answer
- C withAttribute() returns a new request; without $request = $request->withAttribute(...) the unchanged request is passed on
Explanation
PSR-7 requests are immutable: every with*() method returns a modified copy and leaves the original untouched. If the result is not assigned, the old request is passed to the next handler and the attribute is lost. The correct code is $request = $request->withAttribute('myext.region', $region); after that all following middlewares and the controller see the attribute. There is no registration of attributes, and dots in the name are common, the Core uses e.g. frontend.user.