dso407787736 2015-10-25 10: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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?