Laravel5 Learning Four: the operation mechanism of facade

Source: Internet
Author: User

What is facades

Official documentation says:

facades provides a static interface to classes that can be used in the application's service container . Laravel comes with many facades, and even you may have used them unknowingly! Laravel's "facades" acts as a static proxy for the underlying class within the IoC container, providing a concise and expressive advantage, while maintaining a higher testability and elasticity than traditional static methods.

To tell the truth, this passage is really not like the human language, I am ready to disassemble.
First of all, facades is a class, what is a class? It is a static proxy for the underlying class.
Second, facades can be obtained directly from the IOC container, corresponding to the laravel5 means that it can be used directly, it already exists in the app container.
Finally, why should there be facades this stuff? The first advantage is that it provides a more concise and easy-to-express syntax. This is not the main reason, personally think the main reason is: ease of testability and reduce coupling (the advantages of proxy mode)

use examples to illustrate

PS: This part of the content is referenced from the network: original address

We know that $app is a special global variable in Laravel, which is an object instance of the IOC container when it is initially launched.
So if we're going to take an instance out of the IOC container, it's written like this:

$value$app->make(‘cache‘)->get(‘key‘);

Since $app implements PHP's Arrayaccess interface, it can be more simplified and written as:

$value$app[‘cache‘]->get(‘key‘);

You can even write this in the form of a function:

$value = app(‘cache‘)->get(‘key‘);

This time facace out to say, I can write this:

$value = Cache::get(‘key‘);
Operational Mechanism Analysis
    • Facades namespaces
      Believe that as long as play laravel people, will see when encountered facades , import namespaces, just need to use use XXX . You don't have to write the full namespace. Why is this?
      The namespace of the normal class should be written like this:
useIlluminate\Support\Facades\Cache;

facades only need to write this:

useCache;

Why is this possible? I looked at some of its running processes, parsing the following:

The key point in this picture is: The automatic loading method registered in Aliasloader, which uses our array in the app.php configuration aliases , through which we can find the corresponding real class file, and then complete the instantiation of the object.

    • How to build your own facades
      PS: Or the Cache as an example

The first thing to build is the cache, the class itself, to implement its function. The corresponding files here are:
Illuminate\Cache\CacheManager(Note: Because the cache has multiple implementations, it is possible to obtain the desired cache implementation through the manager.) )

When the cache class is written, we will bind it to a service provider, where the service provider location is:
Illuminate\Cache\CacheServiceProvider, the binding part of the code is:

publicfunction register(){    $this->app->singleton(‘cache‘function ($app) {        returnnew CacheManager($app);    });    ......    }

It is important to note that binding, why use the ' cache ' string, will be described later. When the service provider is written, the service provider needs to be configured in the app.php providers array, where the code is as follows:

' providers '= [/ * * Laravel Framework Service Providers ... * *Illuminate\foundation\providers\artisanserviceprovider:: class, illuminate\Auth\authserviceprovider::class,  Illuminate\broadcasting\broadcastserviceprovider::class, illuminate \Bus\busserviceprovider::class, illuminate\Cache\ cacheserviceprovider::class, ...]

After completing the following steps, you can build the facades class, where the facades of the Cache is located Illuminate\Support\Facades\Cache .

<?phpnamespace Illuminate\ Support\Facades;/** * @see \illuminate\cache\cachemanager * @see \illuminate\cache\repository * * class Cache extends facade{    /** * Get The registered name of the component. * * @return string * *    protected Static  function getfacadeaccessor() {        return ' Cache '; }}

All facades classes need to inherit from the facades and implement the getFacadeAccessor method. Its job is to define what to parse from the container. That is to say, if we bind the time, write is cache , then here also need to return cache , otherwise parsing, will not be found. You can try to change to another name.
The final step requires app.php an array in the configuration aliases , which involves automatic loading of the facades . Is the picture that was first drawn.

' aliases '= [' App '= = Illuminate\support\facades\app::class, ‘Artisan' =Illuminate\ Support\Facades\Artisan::class, ‘Auth' =Illuminate\ Support\Facades\Auth::class, ‘Blade' =Illuminate\ Support\Facades\Blade::class, ‘Bus' =Illuminate\ Support\Facades\Bus::class, ‘Cache' =Illuminate\ Support\Facades\Cache::class,    ......    ]

Here, facades 's running parsing mechanism, and how to create their own facades have been explained.
Hope to everyone has help, reprint please indicate the source!

Laravel5 Learning Four: the operation mechanism of facade

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.