Home > PHP Framework > Laravel > The difference between laravel facade and service provider

The difference between laravel facade and service provider

藏色散人
Release: 2019-08-06 11:16:04
Original
2581 people have browsed it

The difference between laravel facade and service provider

The difference between laravel facade mode and service provider

Take Laravel’s own file system as an example, in config In the providers array of the configuration file of /app.php, a service provider is registered:

Illuminate\Filesystem\FilesystemServiceProvider::class,
Copy after login

A facade is defined in the alias array:

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

Through these two steps, we will It is very convenient to use the file system-related operations provided by Laravel, and the calling form is very simple, such as:

File::exist ($path),判断文件是否存在。
File::get ($path, $lock = false),获取一个文件的内容。
File::append ($path, $data),把内容追加到一个文件末尾。
File::files ($directory),获取一个目录下所有文件。
Copy after login

So how is this done? Let’s talk about Laravel’s service provider and facade mode respectively.

Service Provider

Let’s take a look at the definition first:

The service provider is the center where all Laravel applications are started. All core Laravel services, including your own applications, are started through service providers.

In the file system service provider, located at /vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemServiceProvider.php, the register method can see that a singleton is bound :

protected function registerNativeFilesystem()
{
    $this->app->singleton('files', function () {
        return new Filesystem;
    });
}
Copy after login

This singleton is the singleton mode of the Filesystem class. Of course, this service provider can also bind other singletons or do more things. We only study the principle of the File::exist () calling method here.

So there is a single instance of files, which is actually an instance of the Filesystem class.

At this time, if there is no Facade, you can also call the method of the Filesystem instance, that is, call it like this:

app(‘files’)->exist($path)
Copy after login

Okay, now let’s talk about the Facade.

Facade Facade pattern

Let’s take a look at the introduction first:

Facades /fəˈsäd/ provides a "static" interface for classes available in the application's service container. Laravel comes with many facades that can be used to access almost all of its services. Laravel facades are "static proxies" for base classes in the service container. Compared with traditional static method calls, facades provide a simpler and richer syntax, while also having better testability and scalability.

At the beginning of this article, we mentioned that the alias array defines a File. The specific class is

Illuminate\Support\Facades\File::class,
Copy after login

. Its content is:

class File extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'files';
    }
}
Copy after login

It actually returns a name. Note that Isn't this name files the name of the singleton pattern just bound? That's right.

In this way, you can use the File alias or facade to call methods in this Filesystem instance.

Through this article, I hope everyone can understand the relationship between service providers, Facade, and instances of the actually called classes.

For more Laravel related technical articles, please visit the Laravel Framework Getting Started Tutorial column to learn!

The above is the detailed content of The difference between laravel facade and service provider. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template