Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between contract and facade in laravel

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report