Introduction:
- Creator of Laravel, Taylor Otwell brings awesome features in Laravel 5 called Contracts.
- Contracts are a set of interfaces which helps a developer to make code loosely coupled base OR in another term, we can say Contracts are interfaces that provide core services.
- So in common, Laravel Contracts is a wrapper of different interfaces that property of `illuminate/contracts`.
Now what are the ‘Facades’?:
Facades also interface and like a Contracts so let’s first understand what are the Facades.
- Do they have any similarity or relationship or anything else? Let’s understand something about Facades first.
- Facades are also static interfaces that provide access methods to the classes of service containers.
- We can use Facades `typehint` like we already implement the route.
Route::bind('mystore', function ($id){
return App\Store::get();
});
In above code, Facades solved the contracts out of service container. But the quality of Contracts is we can define conspicuous dependencies for your classes and make your application more loosely coupled. Here Facades work fine but in some case, we want to create something extra, you need to use Contracts.
How to write?
Contracts implementation is very simple. Let’s understand the Contracts implementation with sample example:
Suppose I have a ‘StoreController’ with that I will maintain my all store list and for that, I need insert these data into the database. So what I will do, I’ll create an eloquent model ‘Store’ and bind it with a route into route.php.
After that, I will use that model ‘Store’ all CRUD operations using the resource. In between, first I check whether the user is logged in OR not? If logged in then only can able act on model CRUD otherwise not. Example code:
public function insert(Request $request)
{
if (Auth::check()) {
// If user is logged in...
}
}
From above code, I used Facades `Auth` to check the user is logged in OR not? So here if I don’t want more decoupled state it works fine and it’s what? code with SOLID principle and here Contracts comes for the solution. For that what I do? just inject ‘Auth’ Facade through constructor:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard as Auth;
class StoreController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
protected $auth;
public function __construct(Auth $auth) {//Inject `Auth` Facade...
$this->auth = $auth;
}
public function insert(Request $request)
{
$this->auth->attempt();
}
}
As you see, when we use this line of code in our ‘StoreController’: use Illuminate\Auth\Guard as Auth;`
We actually inject an instance based on concretion, not an abstraction. Whenever we call any methods in this instance it is aware of your framework. But we need to make it completely unaware of our Framework and become loosely coupled.
To do that we just have to change one line of code in our ‘StoreController’ instead of that line of code just change `use Illuminate\Auth\Guard as Auth;` to `use Illuminate\Contracts\Auth\Guard as Auth;` and do not need to make change the store() method. This is what loosely coupled provided by Contracts.
When to use?
- In some situation, many developers have use facades and others have use contracts and its depends on developers logic and them test and like as developer teams. contracts and facades both are used to create Laravel applications.You will look few practical differences when your class keeping responsibilities.
- In some case, code requirement is changed to use some specific code/package/service what we do is? we need to change the controller code. but this is the correct way? No. because this will consume our time to re-code that. But is we use that contract and implement that method like service which adheres to the contract and implements those methods. Let’s understand with an example to more clarity:
I have created a `ShippingServiceContract` with methods like `getShippingRegions()`, `getShippingCost()`, `getShippingTrackStatus()`…etc like all basic methods. And create `BludartDeliveryService` which again adhere to Contract and implements those methods.
Now I will user this `ShippingServiceContract` Contract by injecting it into my controller which is called dependency injection and start using it. After few days, I think, I don’t want to use Bludart and for replacement, I need to use some other shipping provider. So now what I need to do is, just create `SomeOtherDeliveryService` that also adhere to the Contract and change binding from `SomeOtherDeliveryService` to `BludartDeliveryService`, and no need to change the controller code that’s it.
For the binding between contract and implementation, we can use a service provider such as `app/Providers/AppServiceProvider.php`
Conclusion:
The Laravel Contracts package provides a special way to extend Laravel core components explicitly and provides a stable API for developers to access the framework’s behaviour.
Hope this blog is a quick and easy guide for Laravel Contracts. Need more assistance regarding Laravel Development Services Contact us now!