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.
What is the difference between contracts and facades in laravel
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!

Laravel stands out by simplifying the web development process and delivering powerful features. Its advantages include: 1) concise syntax and powerful ORM system, 2) efficient routing and authentication system, 3) rich third-party library support, allowing developers to focus on writing elegant code and improve development efficiency.

Laravelispredominantlyabackendframework,designedforserver-sidelogic,databasemanagement,andAPIdevelopment,thoughitalsosupportsfrontenddevelopmentwithBladetemplates.

Laravel and Python have their own advantages and disadvantages in terms of performance and scalability. Laravel improves performance through asynchronous processing and queueing systems, but due to PHP limitations, there may be bottlenecks when high concurrency is present; Python performs well with the asynchronous framework and a powerful library ecosystem, but is affected by GIL in a multi-threaded environment.

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

Laravel can be used for front-end development. 1) Use the Blade template engine to generate HTML. 2) Integrate Vite to manage front-end resources. 3) Build SPA, PWA or static website. 4) Combine routing, middleware and EloquentORM to create a complete web application.

PHP and Laravel can be used to build efficient server-side applications. 1.PHP is an open source scripting language suitable for web development. 2.Laravel provides routing, controller, EloquentORM, Blade template engine and other functions to simplify development. 3. Improve application performance and security through caching, code optimization and security measures. 4. Test and deployment strategies to ensure stable operation of applications.

Laravel and Python have their own advantages and disadvantages in terms of learning curve and ease of use. Laravel is suitable for rapid development of web applications. The learning curve is relatively flat, but it takes time to master advanced functions. Python's grammar is concise and the learning curve is flat, but dynamic type systems need to be cautious.

Laravel's advantages in back-end development include: 1) elegant syntax and EloquentORM simplify the development process; 2) rich ecosystem and active community support; 3) improved development efficiency and code quality. Laravel's design allows developers to develop more efficiently and improve code quality through its powerful features and tools.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor