Introduction to service providers and facade models in Laravel, laravel providers

Source: Internet
Author: User

Introduction to service providers and facade models in Laravel, laravel providers

Preface

In laravel, we may need to create a folder to store class files, or use the laravel service provider.

In fact, the two are slightly different. The main difference is that the former will depend on the Business Code. Imagine if a controller references many custom class files, we can imagine how many dependencies will be generated, so we can use the service provider method to register classes in laravel containers. In this way, we can manage dependencies in a separate configuration file, logic and later maintenance will be much more convenient.

The main reason for using the facade is that you do not need to instantiate the class. You can use the static method to restore the class method, which is convenient to use. However, this method also has disadvantages, for example, you cannot directly jump to the corresponding method or intuitively understand the usage of this method. Personal Development may not be affected, in fact, it may make people a little dizzy.

Take the file system provided by Laravel as an example. In the providers array of the config/app. php configuration file, 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 can easily use the file system-related operations provided by Laravel, and the calling format is very simple, such:

  • File::exist($path)To determine whether a file exists.
  • File::get($path, $lock = false)To obtain the content of a file.
  • File::append($path, $data)To append the content to the end of a file.
  • File::files($directory)To obtain all files in a directory.

So how is this done? The following describes the Laravel service provider and facade model respectively.

Service Provider

Let's take a look at the definition:

The service provider is the center where all Laravel applications start. Your own applications and all Laravel core services are started through the service provider.

In the file system service provider, the location is/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemServiceProvider. php. The register method shows that a singleton is bound:

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

This Singleton mode is the singleton mode of the Filesystem class. Of course, this service provider can also bind other Singleton instances or do more. Here we only studyFile::exist() The principle of this call method.

In this case, there is a file Singleton, which is actually an instance of the Filesystem class.

In this case, if Facade is not available, you can call the method of the Filesystem instance as follows:

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

Now let's start with Facade.

Facade Mode

Let's take a look at the introduction:

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

In this article, 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 this name is files, isn't it the name of the newly bound Singleton mode? Yes.

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

Through this article, we hope you can understand the relationship between the service provider, Facade, and actual called class instances.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.