Laravel Contracts


Laravel Contracts – The Laravel Contracts is used to set of interfaces that define the core services provided by the framework.


Laravel Contracts.

Let us understand how to use laravel Contracts.

Function:-

There are some followings function available in laravel Contracts.

  • 1. Introduction.
  • 2. When To Use Contracts.
  • 3. How To Use Contracts.
  • 4. Contract Reference.

1. Introduction.

Laravel’s Contracts are a set of interfaces that define the core services provided by the framework. For example, a Illuminate/Contracts/Queue/Queue contract defines the methods needed for queueing jobs, while the Illuminate/Contracts/mail/Mailer contract defines the methods needed for sending e-mail.

All contract has a corresponding implementation provided by the framework. Like, Laravel provides a queue implementation with a variety of drivers, and a mailer implementation that is powered by SwiftMailer.

Laravel Contracts live in their own GitHub repository. This provides a quick reference point for all available contracts, as well as a single, decoupled package that may be utilized by package developers.

Contracts Vs. Facades

Laravel’s facades and helper functions provide a way of utilizing Laravel’s services without needing to type-hint and resolve contracts out of the service container. In most cases, each facade has an equivalent contract.

Most application will be fine regardless of whether you prefer facades or contracts. However, if you are building a package, you should strongly consider using contracts since they will be easier to test in a package context.

2. When To Use Contracts.

Contracts or facades will come down to personal taste and the tastes of your development team. Both contracts and facades can be used to create robust, well-tested Laravel applications. As long as you are keeping your class’ responsibilities focused, you will notice very few practical differences between using contracts and facades.

Loose Coupling

First, let’s review some code that is tightly coupled to a cache implementation. Consider the following.

Let’s look at a simple example.

<?php
namespace App\Orders;

class Repository
{    
    protected $cache;   
    public function __construct(\SomePackage\Cache\Memcached $cache)
    {
        $this->cache = $cache;
    }   
    public function find($id)
    {
        if ($this->cache->has($id))    {
            //
        }
    }
}

In this class, the code is tightly coupled to a given cache implementation. It is tightly coupled because we are depending on a concrete Cache class from a package vendor. If the API of that package changes our code must change as well.

Instead of this approach, we can improve our code by depending on a simple, vendor agnostic interface.

Let’s look at a simple example.

<?php
namespace App\Orders;

use Illuminate\Contracts\Cache\Repository as Cache;

class Repository
{
    
    protected $cache;
    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }
}

Now the code is not coupled to any specific vendor, or even Laravel. Since the contracts package contains no implementation and no dependencies, you may easily write an alternative implementation of any given contract, allowing you to replace your cache implementation without modifying any of your cache consuming code.

Simplicity

When all of Laravel’s services are neatly defined within simple interfaces, it is very easy to determine the functionality offered by a given service. The contracts serve as succinct documentation to the framework’s features.

3. How To Use Contracts.

Many types of classes in Laravel are resolved through the service container, including controller, event listeners, middleware, queued jobs and even route Closures. So, to get an implementation of a contract, you can just type-hint the interface in the constructor of the class being resolved.

Let’s look at a simple example.

<?php

namespace App\Listeners;

use App\User;
use App\Events\OrderWasPlaced;
use Illuminate\Contracts\Redis\Database;

class CacheOrderInformation
{
   
    protected $redis;
    public function __construct(Database $redis)
    {
        $this->redis = $redis;
    }
    public function handle(OrderWasPlaced $event)
    {
        //
    }
}

When the event listener is resolved, the service container will read the type-hints on the constructor of the class, and inject the appropriate value.

4. Contract Reference.

This table provides a quick reference to all of the Laravel contracts and their equivalent facades.

Contract Reference facade
Illuminate/Contracts/Auth/Factory Auth
Illuminate/Contracts/Auth/PasswordBroker Password
Illuminate/Contracts/Bus/Dispatcher Bus
Illuminate/Contracts/Broadcasting/Broadcaster
Illuminate/Contracts/Cache/Repository Cache
Illuminate/Contracts/Cache/Factory Cache::driver()
Illuminate/Contracts/Config/Factory Config
Illuminate/Contracts/Container/Container App
Illuminate/Contracts/Cookie/Factory Cookie
Illuminate/Contracts/Cookie/QueueingFactory Cookie::queue()
Illuminate/Contracts/Encryption/Encrypter Crypt
Illuminate/Contracts/Events/Dispatcher Event
Illuminate/Contracts/Filesystem/Cloud
Illuminate/Contracts/Filesystem/Factory File
Illuminate/Contracts/Filesystem/Filesystem File
Illuminate/Contracts/Foundation/Application App
Illuminate/Contracts/Hashing/Hasher Hash
Illuminate/Contracts/Logging/Log Log
Illuminate/Contracts/Mail/MailQueue Mail::queue()
Illuminate/Contracts/Mail/Mailer Mail
Illuminate/Contracts/Queue/Factory Queue::driver()
Illuminate/Contracts/Queue/Queue Queue
Illuminate/Contracts/Redis/Database Redis
Illuminate/Contracts/Routing/Registrar Route
Illuminate/Contracts/Routing/ResponseFactory Response
Illuminate/Contracts/Routing/UrlGenerator URL
Illuminate/Contracts/Support/Arrayable
Illuminate/Contracts/Support/Jsonable
Illuminate/Contracts/Support/Renderable
Illuminate/Contracts/Validation/Factory Validator::make()
Illuminate/Contracts/Validation/Validator
Illuminate/Contracts/View/Factory View::make()
Illuminate/Contracts/View/View

Advertisements

Add Comment

đź“– Read More