. This article mainly introduces the related contents of Laravel 5.5 core architecture, and share it for your reference learning. Here's not much to say. Let's have a detailed introduction.

dependency injection method was introduced to the component name, the framework will automatically instantiated within the method can directly use

as the most commonly used requert

service container in fact, Laravel is the core of a IoC container, the core of Laravel itself is very light, and not what is a wonder of the substantive application function. Many people use a variety of functional modules such as Route, Eloquent ORM (routing) (database ORM component) and Request (request) and Response (response) and so on, are actually the core and independent module provided by these classes from registration to instantiate, eventually you use, is the Laravel service container responsible for.

service provider is mainly divided into two parts, register (Registration) and boot (


, the service provider of a class to be able to extract the container, you must first register to the container. Since Laravel calls this container a service container, we need to register and bind the service to the container if we need a service. Then the thing that provides services and binds the service to the container is the Service Provider.

4.1, their new validate

4.2 validateProvider

4.3, binding validate to Provider

 < PHP namespace AppProviders; use? IlluminateSupportServiceProvider; class ValidateProvider extends ServiceProvider Bootstrap the application services. {/ * * * * * @return void * / public function (boot) {} / * * / / * Register the Application services. * * @return void * / public function (register) {$this-> app-> bind ('valicate', function) (new (Validate) {return};});}

4.4, Provider



4.6, success!

facade), the facade is used to provide a unified interface, for example, no matter which you use cache, redis or Memcache, the client can use cache:: get (value) to obtain, for the concrete is to use redis or memcahe, see you in the sevice provider which bound. Cache:: (get) implementations, getFacadeAccessor inherits the Facade method, you return to the bound in the container in the key, such as cache, then Facade will use PHP (__callstatic), magic variable logic inside the callstatic from the analysis of cache bound container service, service provider is bound above who for example,

5.1 config/app.php mail


5.2, the only return a mailer

5.3, if you call its send method, there is no word will go to callstatic The method of

5.4, this method will get an instance of mailer, namely app ('mailer')

5.5, this example can call the send method mailer class

Laravel of the contract A contract is a set of interfaces that define the core services provided by the framework. For example, IlluminateContractsQueueQueue contracts define the methods needed for queue tasks, while IlluminateContractsMailMailer contracts define the way to send e-mail. The framework provides a corresponding implementation for each contract. The advantage of

is to realize the low coupling and simplicity of the program.

low coupling #

first let us look at some of the high coupling cache implementations of the code. As follows:

 < PHP namespace AppOrders; class? Repository {/ * * * cache instances. Protected * $cache; / * * * create a warehouse instance. * * @param SomePackageCacheMemcached $cache * @return void * public function __construct (SomePackageCacheMemcached $cache) {$this-> cache = $cache;} / * * * * * @param Id in accordance with the retrieval order int $id Order public function * @return * / find ($ID) {if ($this-> cache-> has ($id)) {}} / /}

in this class, realize high coupling procedure with the given cache. Because we rely on a specific cache class of an extended package. Once the API of the extended package has been changed, our code has to be changed.

, similarly, if we want to replace the underlying cache technology (Memcached) into another cache technology (Redis), we have to revise the repository class again. And the repository class should not know too much about who provides these data or how it is provided.

compared to the above approach, we can use a simple, and the expansion pack independent interface to improve our code:

 < PHP namespace AppOrders; use? IlluminateContractsCacheRepository as Cache; class Repository {/ * * * cache instances. Protected * $cache; / * * * create a warehouse instance. * * @param Cache $cache * @return void * public function __construct (Cache $cache) {$this-> cache = $cache;}}

now, after the change of the code have no any expansion pack even Laravel coupling. The contract extension package does not contain any implementations and dependencies. You can easily write any alternative implementation of a given contract, so that the cache implementation can be replaced without modifying any code about cache consumption.

all above is the summary of this article, I hope this paper has some reference value of learning to learn or work, if in doubt, you can exchange messages, thank you for the support of the script.

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Deep understanding of the Laravel 5.5 core architecture | Script Home

You may also be interested in these articles!