Home> PHP Framework> Laravel> body text

Laravel - Contracts

王林
Release: 2024-08-27 10:50:50
Original
518 people have browsed it

Laravel contracts are a set of interfaces with various functionalities and core services provided by the framework.

For example,IlluminateContractsQueueQueuecontract uses a method which is needed for queuing jobs andIlluminateContractsMailMaileruses the method for sending emails.

Every contract defined includes corresponding implementation of the framework. All the Laravel contracts are available in the GitHub repository as mentioned below −

https://github.com/illuminate/contracts

This repository provides a variety of contracts available in the Laravel framework which can be downloaded and used accordingly.

Important Points

While working with Laravel contracts, please note the following important points −

  • It is mandatory to define facades in the constructor of a class.

  • Contracts are explicitly defined in the classes and you need not define the contracts in constructors.

Example

Consider the contract used for Authorization in Laravel which is mentioned below −


        
Copy after login

The contract uses a function can which includes aparameternamedabilityandargumentswhich uses the user identification in the form of anarray.

You will have to define a contract as shown in the syntax below −

interface 
Copy after login

Contracts are used like facades for creating robust, well-tested Laravel applications. There are variouspractical differenceswith usage of contracts and facades.

The following code shows using a contract for caching a repository −

cache = $cache; } }
Copy after login

Contract contains no implementation and new dependencies; it is easy to write an alternative implementation of a specified contract, thus a user can replace cache implementation without modifying any code base.

The above is the detailed content of Laravel - Contracts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!