dso407787736 2015-10-25 02:49
浏览 86
已采纳

Laravel中的依赖注入

Coming for a JavaScript and Java background, I find dependency injection in Laravel (or PHP) extremely confusing.

For example (Laravel 5.1), in Middleware/Authenticate the constructor takes a Guard object. I have search the codebase and the class Authenticate is never instantiated.

So where is the Guard coming from then?

Thanks, SK.

  • 写回答

2条回答 默认 最新

  • douke9379 2015-10-25 02:56
    关注

    Look at the laravel documentation:

    Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods. http://laravel.com/docs/5.1/container

    You can instantiate Guard in your code or fetch instance of Guard from somewere and pass it to Middleware/Authenticate constructor.

    EDITED : Guard is injected into the Middleware/Authenticate automatically somewere in laravel framework code at some point. You better take a look on different example of Dependency injection in Laravel where all is visibly in your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部