Laravel - Contracts

Laravel contracts are a set of interfaces with various features and core services provided by the platform.
For example, Illuminate\Contracts\Queue\Queue contract uses a method that is required for job queues, and Illuminate\Contracts\Mail\Mailer uses a method for sending emails.
Each defined contract includes a corresponding framework implementation. All Laravel contracts are available in the GitHub repository as given below −
https://github.com/illuminate/contracts
This repository provides many contracts available in the Laravel framework that can be downloaded and used appropriately.
Important Points
When working with Laravel contracts, pay attention to the following important points:
-
Be sure to define facades in the class constructor.
-
Contracts are explicitly defined in classes, and you don't have to define contracts in constructors.
Be sure to define facades in the class constructor.
Contracts are explicitly defined in classes, and you don't have to define contracts in constructors.
example
Consider the contract used for authorization in Laravel which is given below −
<? php namespace Illuminate \Contracts\Auth\Access ; interface Authorizable { /** * Determine if the entity has a given ability. * * @param string $ability * @param array|mixed $arguments * @return bool */ public function can ( $ability , $arguments = []); }
The contract uses the can function, which includes a parameter named Ability and arguments that use the user's identity in the form of an array .
You will have to define the contract as shown in the syntax below −
interface <contract-name>
Contracts are used as facades for building robust, well-tested Laravel applications. There are various practical differences with the use of contracts and facades.
The following code shows the use of a storage caching contract:
<?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; } }
The contract contains no implementation and no new dependencies; It is easy to write an alternative implementation of the specified contract, thus the user can replace the cache implementation without changing any code base.