Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between contract and facade in laravel

Shulou Source: shulou.com Published: 2022-06-01 17:12:13 10月01日 Update

Today, the editor will share with you the relevant knowledge points about the difference between contract and facade in laravel. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

The difference: 1, the contract refers to a series of interfaces that define the core services provided by the framework, while the facade provides the role of binding objects in a static class call container; 2, the contract can be regarded as the method definition interface of the framework function, storing most methods, while the facade is to provide methods, provide static calls, and do not need to inject interfaces.

The operating environment of this tutorial: windows10 system, Laravel6 version, DELL G3 computer.

What is the difference between contract and facade in laravel

Introduction

Laravel's Contracts is 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 jobs, while the illuste\ Contracts\ Mail\ Mailer contract defines the methods required to send messages. And Contracts is also called contract.

In our previous study, we learned a lot of helper functions and simple methods provided by Facades facade, which can be implemented without type prompts. In most cases, each facade has a corresponding contract method, which, unlike facades, allows you to display dependencies for class definitions.

When to use the contract

As discussed elsewhere, many decisions to use a contract or Facades will depend on individual preferences 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 singularity of the responsibilities of the class, you will find that the actual difference between using a contract or Facades is actually very small.

How to use the contract

So, how to realize the contract? It's actually very simple.

Many classes in Laravel are parsed through service containers, including controllers, event listeners, middleware, queue tasks, and even routing closures. Therefore, to implement the contract, you only need to "type prompt" interface in the constructor of the class being parsed.

For example, view this event listener:

Tags: Contracts interfaces methods facades frameworks functions knowledge articles services events that is core static different listeners content functions containers types faceted Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Xiaomi NVidia OPPO Reno