Laravel's IOC design

Source: Internet
Author: User
Tags aliases
Yun Zhihui (Beijing) Technology Co., Ltd.

I. Introduction to the IOC concept

The inversion of Control (IOC) mode (also known as di:dependencyinjection) is the inversion Ofcontrol, which controls inversion. In Java development, IOC means handing out your designed classes to the system rather than controlling them within your class. This is called control inversion.

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 be called you."

So, how is the IOC pattern implemented in the Laravel framework?

Second, the implementation of IOC mode in Laravel framework

1. Flowchart of request implementation

In the request flowchart you can see that there are two more important files start.php and application.php, where global variables are introduced in the start.php file $app = new Illuminate\foundation\application Note that the global variable $app is the "housekeeper" that solves all the injection dependencies, storing the tool classes used in the application in advance and extracting them whenever they are used.

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-managed object.

PHP's standard interface arrayaccess contains four properties:

Here is the 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 properties in the container class $bindings and $instances are used to store the tool class instances 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: Route::p ost (' url ', ' Controller@method ') in the usual way of routing;

Registering the service in the $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 different cache storage mode by changing the business logic code to complete.

  • 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.