What is a Contract
Laravel’s “Contracts” are a set of interfaces that define the core services provided by the framework. For example, theIlluminate\Contracts\Queue\Queue contract defines the methods needed for queueing jobs, and the Illuminate\Contracts\Mail\Mailer contract defines the methods needed for sending mail.
Each contract has a corresponding implementation provided by the framework. For example, Laravel provides queue implementations for various drivers and a mailer implementation using Symfony Mailer.
All Laravel Contracts live in their own GitHub repository. This provides a quick reference for all available contracts and is a single, decoupled package you can use when building packages that interact with Laravel services.
A contract is just an interface. It works exactly like any other PHP interface. Laravel provides an implementation of the interface and injects it via the service container.
The difference between Contracts and facades
Facades and helper functions provide a simple way to use Laravel’s services without needing to type-hint and resolve contracts from the service container. In most cases, each facade has an equivalent contract. The main differences between facades and contracts are:When to use Contracts
Whether to use contracts or facades is a matter of personal or team preference. Both contracts and facades can be used to build robust, testable Laravel applications. Contracts and facades are not mutually exclusive. Some parts of your application can use facades while others depend on contracts. Contracts are particularly useful in the following cases:- When building a package that integrates with multiple PHP frameworks — By using the
illuminate/contractspackage to define your integration with Laravel services, you don’t have to require Laravel’s concrete implementation in yourcomposer.json. - When you want to make dependencies explicit — Just by looking at the constructor, you can see at a glance what a class depends on.
- When you want to swap implementations — It’s easier to swap in a different implementation via the service container.
How to use Contracts
How do you get an implementation of a contract? It’s actually very simple. Many types of classes in Laravel—including controllers, event listeners, middleware, queued jobs, and even route closures—are resolved via the service container. So to get an implementation of a contract, you just “type-hint” the interface in the constructor of the class being resolved. For example, look at the following event listener.Creating a custom Contract
By creating your own contracts, you can make the dependencies between components in your application explicit.1
Define the interface
Create the interface in the
app/Contracts directory.2
Create the implementation
Create a class that implements the contract.
3
Bind them in a service provider
Bind the contract to the implementation in a service provider.
4
Receive the injection via a type hint
Type-hint it in the constructor of a controller or other class.
Key Contracts reference
Here’s a partial mapping of commonly used contracts to their corresponding facades:
You can find a complete list of contracts in the illuminate/contracts repository.
Next steps
Facades
Learn how facades work and how to test them.