【问题标题】:Create contracts in laravel 5.4在 laravel 5.4 中创建合约
【发布时间】:2017-02-02 05:26:49
【问题描述】:

laravel.com 上的文档不足。谁能指导我如何从头开始在Laravel 创建合同。

我需要在Laravel 中实施合同。现在,我正在使用Laravel 5.4

【问题讨论】:

    标签: laravel contract laravel-5.4


    【解决方案1】:

    Contract 只是php interfaces 的花哨名称。我们一直在使用它们,这并不是什么新鲜事物。

    Contracts/Interfaces 帮助我们维护一个松散耦合的代码库。请参阅下面文档中的示例。

    <?php
    
    namespace App\Orders;
    
    class Repository
    {
        /**
         * The cache instance.
         */
        protected $cache;
    
        /**
         * Create a new repository instance.
         *
         * @param  \SomePackage\Cache\Memcached  $cache
         * @return void
         */
        public function __construct(\SomePackage\Cache\Memcached $cache)
        {
            $this->cache = $cache;
        }
    
        /**
         * Retrieve an Order by ID.
         *
         * @param  int  $id
         * @return Order
         */
        public function find($id)
        {
            if ($this->cache->has($id))    {
                //
            }
        }
    }
    

    在这里,当Repository 实例化时,我们应该 提供一个\SomePackage\Cache\Memcached 实例以使代码正常工作。因此我们的代码与\SomePackage\Cache\Memcached 紧密耦合。现在看看下面的代码。

    <?php
    
    namespace App\Orders;
    
    use Illuminate\Contracts\Cache\Repository as Cache;
    
    class Repository
    {
        /**
         * The cache instance.
         */
        protected $cache;
    
        /**
         * Create a new repository instance.
         *
         * @param  Cache  $cache
         * @return void
         */
        public function __construct(Cache $cache)
        {
            $this->cache = $cache;
        }
    }
    

    同样的事情,但现在我们只需要提供一些缓存接口。而在幕后你可以做这样的事情。

    <?php
    
    namespace App\Orders;
    
    use Illuminate\Contracts\Cache\Repository as Cache;
    
    class RedisCache implements Cache {
         // 
    }
    

    当上面Repository实例化时,php会查看Illuminate\Contracts\Cache\Repository,它已经被RedisCache类实现了。

    【讨论】:

      【解决方案2】:

      恐怕Gayan's answer 需要进一步说明才能点击Rajan's question

      是的 Gayan 是正确的,创建 Contract 类基本上意味着创建 php interface

      继续上面的 Cache 示例,如果我们查看它的源代码(您可以在 this Github repo file 找到它),我们可以看到类似这样的内容

      <?php
      
      namespace Illuminate\Contracts\Cache;
      
      use Closure;
      
      interface Repository
      {
          /**
           * Determine if an item exists in the cache.
           *
           * @param  string  $key
           * @return bool
           */
          public function has($key);
      
          /**
           * Retrieve an item from the cache by key.
           *
           * @param  string  $key
           * @param  mixed   $default
           * @return mixed
           */
          public function get($key, $default = null);
      
          // the rest...
      }
      

      如果我们在我们的 laravel 应用中使用这个接口,我们就说它是一个“合约”。它声明了一个类如果实现了这个接口应该有什么方法/属性。例如在我们的应用中...

      <?php
      
      namespace App\Whatever;
      
      use Illuminate\Contracts\Cache\Repository;
      
      class Foo implements Repository {
           // 
      }
      

      那么Foo 类将需要具有hasget 方法,以实现Repository 合同中规定的内容。

      【讨论】:

        猜你喜欢
        • 2017-06-29
        • 2017-11-18
        • 2017-11-24
        • 2019-01-17
        • 2017-08-26
        • 1970-01-01
        • 2018-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多