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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog