Home > Article > PHP Framework > What is the difference between contract and facade in laravel
Difference: 1. The contract refers to a series of interfaces provided by the framework that define core services, while the facade provides static interfaces for the classes of the IoC service container; 2. The contract can be regarded as the method definition interface of the framework function. , stores most methods, and the facade provides methods, provides static calls, and does not need to inject interfaces.
The operating environment of this tutorial: Windows 10 system, Laravel 6 version, DELL G3 computer.
Introduction
Laravel’s Contracts are a set of interfaces that are provided by the framework and define core services . For example, the illuste\Contracts\Queue\Queue contract defines the methods required to queue a job, while the illuste\Contracts\Mail\Mailer contract defines the methods required to send mail. Contracts are also called contracts
In our previous study, we learned a lot of auxiliary functions and simple methods provided by the Facades facade, which can implement methods without type hints. In most cases, every Each facade has a corresponding contract method. Unlike facades, contracts allow you to display dependencies for class definitions.
When to use contracts
As discussed elsewhere As discussed, many of the decisions about using Contracts or Facades will come down to personal preference and the preferences of the development team. Both Contracts and Facades can be used to create powerful and well-tested Laravel applications. As long as you focus on the fact that a class should have a single responsibility, you'll find that the actual difference between using contracts or facades is actually very small.
How to use the contract
So, how to implement the contract? It's actually very simple.
Many classes in Laravel are resolved through the service container, including controllers, event listeners, middleware, queue tasks, and even routing closures. Therefore, to implement the contract, you simply "type-hint" the interface in the constructor of the resolved class.
For example, view this event listener:
<?php namespace App\Listeners; use App\Events\OrderWasPlaced; use App\Models\User; use Illuminate\Contracts\Redis\Factory; class CacheOrderInformation { /** * Redis 工厂实现 */ protected $redis; /** * 创建一个事件处理实例 * * @param Factory $redis * @return void */ public function __construct(Factory $redis) { $this->redis = $redis; } /** * 处理事件 * * @param OrderWasPlaced $event * @return void */ public function handle(OrderWasPlaced $event) { // } }
The difference between contract and facade
A contract actually refers to a series of definition cores provided by the framework The interface of the service, the keyword is this interface. Looking at the above code, you can understand what functions this interface provides. We can regard the contract as the method definition interface of the framework function. This interface stores the functions we need to use. Most methods.
The facade also provides methods, but every time we want to use the interface method, we need to inject the corresponding interface, which is very troublesome, so the facade was born to provide static calls. , no need to inject the interface.
[Related recommendations: laravel video tutorial]
The above is the detailed content of What is the difference between contract and facade in laravel. For more information, please follow other related articles on the PHP Chinese website!