Skip to content

Avoid injecting http request via construct really? #50976

Answered by kewne
ol1s asked this question in Q&A
Discussion options

You must be logged in to vote

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.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@marius-mcp
Comment options

Answer selected by ol1s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants