Laravel's IOC design

Source: Internet
Author: User


Yun Zhihui (Beijing) Technology Co., Ltd.

First, Introduction to the IoC concept

control reversal ( ioc ( also known as Di: dependencyinjection) , control inversion. In java development, IoC

The IoC (inversion of Control) is an idea that has arisen in recent years, not just programming ideas. The main is to coordinate the dependencies between the components, while greatly improving the portability of components, and the reuse of components more opportunities. In the traditional implementation, the program internal code to the relationship between the control program. We often use the new keyword to implement a combination of two sets of key relationships, which creates a coupling between components ( a good design that not only implements code reuse, but also decouples the relationships between components ). The IoC solves this problem very well, and it manages the relationship between components from the inside of the program that refers to the external container. This means that the container will dynamically inject a dependency between components in the component at run time. The implementation of the relationship between the control program is given to the external container to complete. It is often said that the Hollywood principle of "Don ' t call us, we'll becalled you."

Well, in How does the Ioc pattern in the Laravel framework be implemented?

Second, the implementation of IOC mode in Laravel framework

1. Flowchart of request Implementation

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/7F/wKiom1WeDa_hUfBHAAHaZrj-BcM907.jpg "title=" QQ picture 20150709135725.png "alt=" Wkiom1weda_hufbhaahazrj-bcm907.jpg "/>

you can see in the request flowchart that there are two more important files start.php and application.php, wherethe global variables are introduced in the start.php file $app = New Illuminate\foundation\application; Note that the global variable $app is for us to solve all the injection dependency of "housekeeper", it will be used in the application of the tool classes stored in advance, when the use of the time can be extracted at any time.

2. managing objects in an array-based manner

The Lluminate\foundation\application class inherits the Laravel container Container and PHP 's standard interface Arrayaccess, thus implementing an array-mode management object.

PHP 's standard interface arrayaccess contains four properties:

Below is definition of arrayaccess:

Interface Arrayaccess

Boolean offsetexists ($index)

Mixed Offsetget ($index)

void Offsetset ($index, $newvalue)

void Offsetunset ($index)

These four abstract methods are rewritten by the container class inherited in the application class , which implements the functions used in the application. The two attributes $bindings and $instances in the Container class are used to store the instance of the tool class used in the actual application. This allows us to invoke the method in the real tool class instance by $app [the alias of the tool class ] or by means of the "tool class alias :: method Name".

GCA, the usual way of routing: Route::p ost (' url ', ' [email protected] ');

registering the service in a $app global variable,laravel is implemented by reading the app/config/app.php configuration and

' Providers ' = Array (

' Illuminate\foundation\providers\artisanserviceprovider ',

' Illuminate\auth\authserviceprovider ',

' Illuminate\cache\cacheserviceprovider ',

' Illuminate\session\commandsserviceprovider ',

' Illuminate\foundation\providers\consolesupportserviceprovider ',

' Illuminate\routing\controllerserviceprovider ',

' Illuminate\cookie\cookieserviceprovider ',

' Illuminate\database\databaseserviceprovider ',

' Illuminate\encryption\encryptionserviceprovider ',

' Illuminate\filesystem\filesystemserviceprovider ',

' Illuminate\hashing\hashserviceprovider ',

' Illuminate\html\htmlserviceprovider ',

' Illuminate\log\logserviceprovider ',

' Illuminate\mail\mailserviceprovider ',

' Illuminate\database\migrationserviceprovider ',

' Illuminate\pagination\paginationserviceprovider ',

' Illuminate\queue\queueserviceprovider ',

' Illuminate\redis\redisserviceprovider ',

' Illuminate\remote\remoteserviceprovider ',

' Illuminate\auth\reminders\reminderserviceprovider ',

' Illuminate\database\seedserviceprovider ',

' Illuminate\session\sessionserviceprovider ',

' Illuminate\translation\translationserviceprovider ',

' Illuminate\validation\validationserviceprovider ',

' Illuminate\view\viewserviceprovider ',

' Illuminate\workbench\workbenchserviceprovider ',

),

through the \illuminate\foundation\start.php file in the

$providers = $config [' providers '];

$app->getproviderrepository ()->load ($app, $providers);

injected into the $app . In the same time, the file is also executed

$aliases = $config [' aliases '];

Aliasloader::getinstance ($aliases)->register ();

This enables you to use aliases to access the services you want to run.

Benefits of using control reversals and aliases:

The same method of invocation can be solved by controlling the inversion in Laravel, but can be implemented in different ways. This way we can change the different ways to solve the problem without changing the code in the business logic.

For example, The cache used by Laravel, which requires only cache::set () in the Business logic code , can be set to cache. However, we do not care about the cache in what way . This is because the $app [' cache '] can hold an instance of Redis, or it can hold an instance of Memcache , which can be determined by a configuration file.

If you use the traditional cache mode, it will be redis::set () or memcache::set () , so we want to change the cache Storage mode is done by changing the business logic code.


Laravel's IOC design

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.