laravel facade with service providers difference

laravel facade difference model and service providers

Laravel comes to the file system, for example, in an array of providers config / app.php profile, the registration of a service provider:

Illuminate\Filesystem\FilesystemServiceProvider::class,

 

   

We define an alias facade in the array:

‘File’ => Illuminate\Support\Facades\File::class,

 

   

By these two steps, we can very easily use Laravel provides operating system-related files, and call in the form of very simple, such as:

File :: exist ($ path), to determine whether a file exists.

File :: get ($ path, $ lock = false), to obtain the contents of a file.

File :: append ($ path, $ data), to append to the end of a file.

File :: files ($ directory), get all the files in a directory.

 

   

So this is how to do it? The following were Laravel talk about service providers and facade patterns.

service provider

First look at the definition:

Where the service provider is the center of all Laravel application startup. Including your own applications, as well as all Laravel core services are launched by the service provider.

In this file system service provider, the location /vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemServiceProvider.php, register method can be seen bound to a single example:

protected function registerNativeFilesystem()

{

    $this->app->singleton('files', function () {

        return new Filesystem;

    });

}

 

   

This embodiment is a single-mode single Filesystem embodiment of this class. Of course, the service provider can also bind other singleton, or do more. We only studied File :: exist () calls the principle of this approach.

In this way we will have a single embodiment files, in fact, an instance of this class Filesystem.

At this point, if not Facade, also can call the method Filesystem this instance, that is called with:

app(‘files’)->exist($path)

 

   

Links: https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g  extraction code: x2p5

free to share, but serious limitations of X, should click on the link or links fail Search plus population group number 518 475 424 .

Well, now began to speak Facade.

Facade Facade pattern

Look under the Description:

Service container application available in the class provides a "static" Interface Facades / fəsäd / is. Laravel comes with many of the facades, it can be used to access almost all of its services. Laravel facades is the service container that base class 'static proxy', compared to the traditional static method call, facades provide a more concise syntax and rich at the same time, there are better testability and scalability.

Beginning of this article talked alias array defines a File, specific class is

Illuminate\Support\Facades\File::class,

 

   

Its contents are:

class File extends Facade

{

    /**

     * Get the registered name of the component.

     *

     * @return string

     */

    protected static function getFacadeAccessor()

    {

        return 'files';

    }

}

 

   

It actually returns a name, note the name of the files, not that the name of the Singleton pattern just binding it? Yes.

As a result, you can use the File alias or facade, to call this method the Filesystem instance.

In this article, I hope you can understand the relationship between service providers, Facade, and examples of actual call class.

Guess you like

Origin www.cnblogs.com/it-3327/p/11743564.html