Replies: 2 comments
-
|
This is actually expected behavior and part of how the router normalizes responses through the pipeline. The first call to Then, This second call acts as a final normalization step to ensure that anything coming out of the middleware stack is still a valid So even if it looks redundant, it’s intentional:
Regarding events like In practice, this doesn’t usually cause issues, but it’s something to keep in mind if you're listening to those events. So overall, this behavior is by design rather than a bug. |
Beta Was this translation helpful? Give feedback.
-
|
Yes, this is intentional by design. The double call exists for a specific and important reason — let me explain each call's role. Why
|
| Call 1 | Call 2 | |
|---|---|---|
| Input | Raw controller return value | Result from entire middleware stack |
| Purpose | Normalize for middleware consumption | Guarantee final response validity |
| Can be skipped? | No — middleware needs a Response | No — middleware may have modified it |
Regarding PreparingResponse / ResponsePrepared events
Yes — these events fire twice as a direct consequence. If you're listening to these events, be aware they will trigger once for the inner normalization and once for the final normalization. In practice this rarely causes issues unless you have side effects in those listeners (e.g., logging or modifying headers), in which case you'd need to guard against the double execution.
This is by design, not a bug, and has been this way since early versions of Laravel's routing layer.
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
11.48.0
Description
I noticed that
prepareResponse()is called twice during a single request in Laravel Router.From what I understand:
runRouteWithinStackafterroute->run().runRoutewith the result returned fromrunRouteWithinStack.Also, this mean
PreparingResponseandResponsePreparedevents will be dispatched twice for one request. Is that also expected behavior by design?Related Code:
Beta Was this translation helpful? Give feedback.
All reactions