【问题标题】:Differences between contracts and facades laravel合同和立面之间的区别 laravel
【发布时间】:2016-02-25 17:09:10
【问题描述】:

自 4 个月以来我一直在做 laravel。我没有发现外观和合同之间的明显区别,因为它们都是一组接口。为什么我要使用外观而不是合同或合同而不是外观?

【问题讨论】:

    标签: oop interface laravel-5.1


    【解决方案1】:

    是使用Facade 还是Contract 的问题归结为您希望如何解析类以及是否要使用接口。

    立面

    • 外观是一个类而不是接口(@98​​7654321@ 是外观示例)。

    • 外观用于从服务容器加载类更方便

    • 要加载的类是门面类的getFacadeAccessor()方法中的determent。

    例子:

    // Without facade - resolving from service container
    app('some_service')->methodName();
    
    // Do the same through facade:
    someService::methodName();
    

    合同

    • 合约是一个接口(@98​​7654322@ 就是一个例子)
    • 契约用于从服务容器加载类更方便AND作为接口
    • 要加载的类在服务容器中确定,见Binding Interfaces To Implementations

    例子:假设类some_service实现了接口Illuminate\Contracts\Config\Repository

    // resolving class directly from service container
    app('some_service')->methodName();
    
    // resolve through binding from contract
    app('Illuminate\Contracts\Config\Repository')->methodName();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 2015-01-13
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2013-12-31
      相关资源
      最近更新 更多