Detailed description of contracts in Laravel5, laravel5contracts_PHP tutorial

Source: Internet
Author: User
Detailed description of contracts in Laravel5, laravel5contracts. In Laravel5, the description of contracts. in laravel5contracts, let's take a look at the definition of contracts in the official document: LaravelsContractsareasetofinterfacesthatdefinethecoreservicesp Laravel5.

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:
<? Php namespace App \ Orders;
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:
<? Php namespace App \ Orders;
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.

For details, 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 p...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.