Previously autowired dependency injection for controller actions was rejected https://github.com/yiisoft/yii2/issues/9476. Now we have it in Yii 3 and I think we can introduce it in Yii 2 as well for the same purpose as Yii 3 style container syntax was introduced - to make it more convenient overall and to make it easier to migrate to Yii 3.
Note to myself. Check https://yiiframework.ru/forum/viewtopic.php?f=27&t=53846 as one of the possible solutions.
this will be great, now \Yii::$app->xxx everywhere :). In lavarel, there are many short functions and instance injections, looks clearly.
Also, to complete the chain, it would be good to return to discussing this issue as well. In my projects, auto-injection of dependencies is very much requested in queue jobs :)
I've been using this for since I created it, been in production for multiple years:
https://github.com/SAM-IT/yii2-magic/blob/master/src/Traits/ActionInjectionTrait.php
Notably it takes care of some extra cases:
$requestedParams so that the debugger works and gives useful informationAdapting your code would be good. Took some time, right? ;)
Do you have some time for a pull request?
Unfortunately I do not; but I'm okay with you copying whatever you want from there verbatim and just put it in the source code.
Also, do you want to implement it as a trait, as a behavior or as part of the base controller class?
Also, please ping me when you've done so and I'll remove it from my library.
Base controller class.
Most helpful comment
I've been using this for since I created it, been in production for multiple years:
https://github.com/SAM-IT/yii2-magic/blob/master/src/Traits/ActionInjectionTrait.php
Notably it takes care of some extra cases:
$requestedParamsso that the debugger works and gives useful information