In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of service providers and facade patterns in Laravel. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Preface
In laravel, when we may need to use the classes we add, we can set up a folder to store the class files, or we can use laravel's service provider to use it.
In fact, there is not much difference between the two. If the former is used, it will have dependencies with the business code. Imagine how many dependencies will be generated if many custom class files are referenced in a controller. So we can use the service provider to register the classes in the laravel container, so that we can manage the dependencies in a separate configuration file. Logic and post-maintenance will also be much more convenient.
The main way to use the facade is that you do not need to instantiate the class, and you can use static methods to access the methods of the class, which is also convenient to use, but this actually has some shortcomings, such as not being able to jump directly to the interior of the corresponding method. Also can not intuitively understand the use of this method, personal development may not have much impact, but if the team development, in fact, it may make people a little dizzy.
Taking the file system that comes with Laravel as an example, a service provider is registered in the providers array of the configuration file of config/app.php:
Illuminate\ Filesystem\ FilesystemServiceProvider::class
A facade is defined in the alias array:
'File' = > Illuminate\ Support\ Facades\ File::class
Through these two steps, we can easily use the operations related to the file system provided by Laravel, and the call form is very simple, such as:
File::exist ($path) to determine whether the file exists.
File::get ($path, $lock = false) to get the contents of a file.
File::append ($path, $data) to append the contents to the end of a file.
File::files ($directory) to get all the files in a directory.
So how do you do that? Let's talk about the service provider and the facade model of Laravel respectively.
Service provider
Take a look at the definition first:
The service provider is the center of all Laravel application launches. Including your own applications, as well as all Laravel core services, are started through the service provider.
In the file system, the service provider, the location / vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemServiceProvider.php,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 pattern of the class Filesystem. Of course, this service provider can also bind other singletons or do more things. We only look at the principle of calling File::exist () here.
So there is a singleton of files, which is actually an instance of the class Filesystem.
At this point, if there is no Facade, you can also call the method of this instance of Filesystem, which is called like this:
App ('files')-> exist ($path)
All right, now let's talk about Facade.
Facade facade mode
Let's take a look at the brief introduction:
Facades / f classes ä d / provides a "static" interface for classes available in the application's service container. Laravel comes with a lot of facades that can be used to access almost all of its services. Laravel facades is the "static proxy" of the base class in the service container. Compared with the traditional static method call, facades not only provides more concise and rich syntax, but also has better testability and extensibility.
At the beginning of this article, we mentioned that the alias array defines a File, and the specific classes are
Illuminate\ Support\ Facades\ File::class
Its content is as follows:
Class File extends Facade {/ * * Get the registered name of the component. * * @ return string * / protected static function getFacadeAccessor () {return 'files';}}
It actually returns a name, and notice that the name files is the name of the singleton pattern that you just bound. That's right.
In this way, you can use the alias or facade of File to call the methods in this Filesystem instance.
Through this article, I hope you can understand the relationship between the service provider, the Facade, and the instance of the class that is actually invoked.
This is the end of the article on "sample analysis of service providers and facade patterns in Laravel". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.