Detailed description of contracts in Laravel5. In Laravel5, the contracts details show a new thing called contracts in Laravel5. So what is it? What is the purpose? How to use it? Let's discuss it. Let's first explain the contracts in Laravel5.
What is new in Laravel5 called contracts? What is the purpose? How to use it? Let's discuss it.
Let's take a look at the definition of contracts in the official document:
Laravel's Contracts are a set of interfaces that define the core services provided by the framework.
It means that Laravel's Contracts is a set of core service interfaces defined by the framework.
That is to say, every Contract is an interface that corresponds to a core framework service.
So what is its significance? The official website also provides a simple explanation: the interface is used for loose coupling and simplicity.
Let alone the truth. let's take a look at how to use contract.
First, browse the contracts interface list:
The code is as follows:
Illuminate \ Contracts \ Auth \ Guard
Illuminate \ Contracts \ Auth \ PasswordBroker
Illuminate \ Contracts \ Bus \ Dispatcher
Illuminate \ Contracts \ Cache \ Repository
Illuminate \ Contracts \ Cache \ Factory
Illuminate \ Contracts \ Config \ Repository
Illuminate \ Contracts \ Container
Illuminate \ Contracts \ Cookie \ Factory
Illuminate \ Contracts \ Cookie \ QueueingFactory
Illuminate \ Contracts \ Encryption \ Encrypter
Illuminate \ Contracts \ Routing \ Registrar
...... Too many, too lazy to continue posting, there are in the official manual. Let's take the contract of Illuminate \ Contracts \ Routing \ Registrar for demonstration.
First, open app/Providers/AppServiceProvider. php. Note The register method:
The code is as follows:
Public function register ()
{
$ This-> app-> bind (
'Illuminate \ Contracts \ Auth \ Registrar ',
'App \ Services \ Registrar'
);
}
$ This-> app is the Application object and container object, through the $ this-> app-> bind method, we bound a class App \ Services \ Registrar that implements the Illuminate \ Contracts \ Auth \ Registrar interface.
Note that Illuminate \ Contracts \ Auth \ Registrar is a contract. App \ Services \ Registrar files are stored in app/Services/Registrar. php.
Next, let's look at the controller class App \ Http \ Controllers \ Auth \ AuthController. we can see that it has the _ construct constructor:
The code is as follows:
Public function _ construct (Guard $ auth, Registrar $ registrar)
{
$ This-> auth = $ auth;
$ This-> registrar = $ registrar;
$ This-> middleware ('guest ', ['login t' => 'getlogout']);
}
It has two parameters. the corresponding class namespace can be seen at the beginning of the script:
The code is as follows:
Use Illuminate \ Contracts \ Auth \ Guard;
Use Illuminate \ Contracts \ Auth \ Registrar;
Both of them are contract, but here we will take Registrar as an example. we noticed that the interface type of $ registrar is specified only through the parameter type, the actual call is actually the App \ Services \ Registrar class, which is the feature of dependency injection, laravel will automatically search for classes or objects that implement the Illuminate \ Contracts \ Auth \ Registrar interface in the container. If yes, it will be retrieved as the actual parameter and uploaded to the constructor.
The entire process can be summarized into two steps:
Register an object that implements the contract interface with the container.
If the contract interface class is specified as the contract parameter type, the framework automatically finds the object that meets the condition.
Let's talk about the benefits of contract.
Loose coupling
The official website provides an example to explain what is tight coupling and why the Contract interface can be loosely coupled.
Let's take a look at the tightly coupled code:
The code is as follows:
Class Repository {
/**
* The cache.
*/
Protected $ cache;
/**
* Create a new repository instance.
*
* @ Param \ SomePackage \ Cache \ Memcached $ cache
* @ Return void
*/
Public function _ construct (\ SomePackage \ Cache \ Memcached $ cache)
{
$ This-> cache = $ cache;
}
/**
* Retrieve an Order by ID.
*
* @ Param int $ id
* @ Return Order
*/
Public function find ($ id)
{
If ($ this-> cache-> has ($ id ))
{
//
}
}
}
We can see that a detailed Cache implementation \ SomePackage \ Cache \ Memcached is injected into the constructor. if you change Redis as the Cache server or change the api method, you need to modify it, if the project is large, you don't know how many changes are needed.
So how does the Contract interface solve this problem? See the code:
The code is as follows:
Use Illuminate \ Contracts \ Cache \ Repository as Cache;
Class Repository {
/**
* Create a new repository instance.
*
* @ Param Cache $ cache
* @ Return void
*/
Public function _ construct (Cache $ cache)
{
$ This-> cache = $ cache;
}
}
Note: the Cache implementation uses an interface, namely contract, Illuminate \ Contracts \ Cache \ Repository, because it is only an interface and does not need to be concerned about whether memcache or redis is behind it.
Simplicity
If all services use the interface definition, you can easily determine the functions required by a service, which is easier to maintain and expand. The contract interface can also be seen as a concise document for ease of reading.
What is the concept of contracts in Laravel5? What is the purpose? How to use it? Let's discuss it. Let's start...