-
Reading through the Laravel Octane docs (https://laravel.com/docs/11.x/octane#request-injection) it seems to suggest that from a best practices perspective:
So I guess this means that the following is NOT recommended:
My question though - is this really the case? The docs then go on to mention an exception:
So should we really avoid injecting the The Laravel Octane docs give an example using a singleton which makes much more sense to me - as in, you don't want a request bound to a singleton with Octane because subsequent requests will receive the original (stale) instance. But, in general, if if I'm not using a singleton is there really anything so bad about injecting the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's not that it's bad per se, it's that the alternatives are usually better, from a modelling perspective. The docs say not only that it is acceptable to type-hint The likely reason is that controllers, like As for why methods and not constructors, it has to do with class lifecycle. |
Beta Was this translation helpful? Give feedback.
It's not that it's bad per se, it's that the alternatives are usually better, from a modelling perspective.
The docs say not only that it is acceptable to type-hint
Request
in controllers, but also specifically says controller methods.The likely reason is that controllers, like
Request
, are HTTP specific. They exist to translate HTTP requests into service calls, so when your form or API changes, you don't have to rewrite your domain logic.As for why methods and not constructors, it has to do with class lifecycle.
If you inject
Request
into the constructor, you then have to create a new controller instance for each request, whereas injecting into the method allows you to reuse the instance.