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,
A facade is defined in the alias array:
‘File’ => Illuminate\Support\Facades\File::class,
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),获取一个目录下所有文件。
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; }); }
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)
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,
. Its content is:
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 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!