_php example of contracts detailed in Laravel5

Source: Internet
Author: User
Tags auth memcached redis

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 collection of core service interfaces provided by the framework.

In other words, each contract is an interface that corresponds to a framework core service.

What's the point of it? The explanation given by the official website is also simple: the interface is used for loose coupling and simplicity.

First, do some dry goods, see how to use Contract

Browse the Contracts interface list first:

Copy Code code 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\container
Illuminate\contracts\cookie\factory
Illuminate\contracts\cookie\queueingfactory
Illuminate\contracts\encryption\encrypter
Illuminate\contracts\routing\registrar

...... Too much, do not bother to continue to paste, the official website manual has. Let's take illuminate\contracts\routing\registrar this contract to demonstrate it.
First, open app/providers/appserviceprovider.php and note the Register method:

Copy Code code as follows:

Public Function Register ()
{
$this->app->bind (
' Illuminate\contracts\auth\registrar ',
' App\services\registrar '
);
}

$this->app is the Application object, also the container object, through $this->app->bind method We bind a realization illuminate\contracts\auth\ Class App\services\registrar of the Registrar interface.

Note that Illuminate\contracts\auth\registrar is a contract. App\services\registrar This class file in app/services/registrar.php.

Then we look at App\http\controllers\auth\authcontroller this controller class and see that it has __construct constructors:

Copy Code code as follows:

Public function __construct (Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;

$this->middleware (' guest ', [' except ' => ' getlogout ']);
}

It has two parameters, and the corresponding class namespace can be seen at the beginning of the script:

Copy Code code as follows:

Use Illuminate\contracts\auth\guard;
Use Illuminate\contracts\auth\registrar;

Both of these are contract, but we'll take registrar here and say, we notice that the interface type of the $registrar is simply indicated by the parameter type, and the actual call is actually app\services\registrar this class, This is the characteristic of dependency injection, Laravel automatically searches the container for the class or object that implements the interface Illuminate\contracts\auth\registrar, and some words are taken out as actual parameters to the constructor.

The entire usage process can be summed up in two steps:

Registers an object that implements the contract interface to the container.
The constructor parameter type is specified as the contract interface class, and the framework automatically finds the object that meets the criteria.
So, again, the benefits of contract.

Loose coupling

The official website gives an example of what a tight coupling is and why the contract interface can be loosely coupled.

Let's look at the tightly coupled code:

Copy Code code 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))
{
//
}
}
}

You can see that a detailed cache implementation \somepackage\cache\memcached is injected into the constructor, and if you change the Redis as a caching server or if the API method changes, you need to modify it, and if the project is large, you don't know how much more needs to be changed.

So, how does the contract interface solve this problem? Please look at the code:

Copy Code code 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 that the cache implementation we use an interface, that is, contract,illuminate\contracts\cache\repository, because it is just an interface, do not need to care whether behind the Memcache or Redis.

of simplicity

If all services use interface definitions, it is easy to determine the functionality that a service needs, easier to maintain and extend, and contract interfaces as a concise document for easy reading.

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.