Using dependency injection over laravel facades

后端 未结 6 1316
鱼传尺愫
鱼传尺愫 2021-01-30 16:48

I have read a number of sources that hint that laravel facade\'s ultimately exist for convenience and that these classes should instead be injected to allow loose coupling. Even

6条回答
  •  孤独总比滥情好
    2021-01-30 17:08

    Class methods that form a part of the routing mechanism in Laravel (middleware, controllers, etc.) also have their type-hints used to inject dependencies - they don't all need to be injected in the constructor. This may help to keep your constructor slim, even though I'm not familiar with any four parameter limit rule of thumb; PSR-2 allows for the method definition to be stretched over multiple lines presumably because it's not uncommon to require more than four parameters.

    In your example you could inject the Request and Validator services in the constructor as a compromise, since they're often used by more than one method.

    As for establishing a consensus - Laravel would have to be more opinionated for applications to be similar enough to utilise a one-size-fits-all approach. An easier call though is that I think facades will go the way of the dodo in a future version.

提交回复
热议问题