4

Documentation on laravel.com is not sufficient. Can any one guide me through how to How To Create contracts in Laravel from scratch.

I need implementation of Contracts in Laravel. Right now, I'm using Laravel 5.4

Vaviloff
  • 16,282
  • 6
  • 48
  • 56
Rajan
  • 105
  • 1
  • 8

2 Answers2

4

Contract is just a fancy name for php interfaces. We have being using them all along and its not a new thing.

Contracts/Interfaces help us to maintain a loosely coupled code base. See the example from doc below.

<?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))    {
            //
        }
    }
}

Here when ever the Repository instantiate we should give a \SomePackage\Cache\Memcached instance in order for code to work. Hence our code is tightly coupled with \SomePackage\Cache\Memcached. Now look at below code.

<?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;
    }
}

Same thing but now we just need to provide some cache interface. And behind the scene you could have done something like this.

<?php

namespace App\Orders;

use Illuminate\Contracts\Cache\Repository as Cache;

class RedisCache implements Cache {
     // 
}

When above Repository instantiate, php will look at the Illuminate\Contracts\Cache\Repository and It has been implemented by RedisCache class.

Gayan
  • 3,614
  • 1
  • 27
  • 34
4

I'm afraid Gayan's answer needs further elaboration to hit Rajan's question.

Yes Gayan is correct that creating a Contract class basically means creating a php interface.

Continuing the Cache example above, if we look into its source code (you can find it at this Github repo file), we can see something like this

<?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...
}

If we are using this interface in our laravel app, it is said to be a "Contract". It is declaring what methods/properties a class should have if it implements this interface. For example in our app...

<?php

namespace App\Whatever;

use Illuminate\Contracts\Cache\Repository;

class Foo implements Repository {
     // 
}

Then class Foo will need to have methods has and get in order to fulfil what has been stated in the Repository contract.

Richard Ng
  • 41
  • 1