doupang4126 2014-12-07 10:12
浏览 57
已采纳

Laravel 5中的合同

Interfaces I understand the reasons for. Single responsibility I understand the reasoning. Basically I do understand why the 'contract' path has been chosen, I'm just struggling with understanding the implementation.

For example the docs give us this example:

<?php namespace App\Orders;

use Illuminate\Contracts\Cache\Repository as Cache;

class Repository {

    /**
     * Create a new repository instance.
     *
     * @param  Cache  $cache
     * @return void
     */
    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }

}

If i were to replace the laravel native cache driver with, let's say, a third party, framework agnostic cache driver, would I then have to write an adapter to adhere to Laravel 5.0 cache contract?

  • 写回答

1条回答 默认 最新

  • dopnpoh056622 2015-04-23 02:31
    关注

    I think you are asking Binding Interfaces To Implementations.

    How to bind an implementation to an interface?

    $this->app->bind('App\Contracts\EventPusher', 'App\Services\PusherEventPusher');
    

    And where does Laravel itself bind the default implementations?

    In 'Illuminate/Foundation/Application.php', it told the container what are the aliases for an abstract keyword:

    'cache' => ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'],
    

    this way, when you write either

    $this->app->make('Illuminate\Cache\CacheManager')
    

    or

    $this->app->make('Illuminate\Contracts\Cache\Factory')
    

    it's basically the same as

    $this->app->make('cache')
    

    And in Illuminate\Cache\CacheServiceProvider.php, it told the container what are the real bindings to the abstract keyword:

    $this->app->singleton('cache', function ($app) {
        return new CacheManager($app);
    });
    

    Let me know if you have other questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路