Brief introduction to service provider and façade mode in Laravel

Source: Internet
Author: User
This article mainly introduces the service providers in Laravel and the use of the façade model of the relevant materials, the text through the detailed sample code to introduce the service providers and the façade model in Laravel, for everyone's study or work has a certain reference learning value, The friends who need to study together.

Objective

In Laravel, we may need to use a class that we have added to create a folder to hold the class file, or to use the Laravel service provider.

In fact, the difference is not small, mainly the former use, will be dependent on the business code, imagine, if a controller refers to a lot of custom class files, then you can imagine how much dependency, so we can use the service provider's way to the Laravel container register class, In this way, it is possible to manage dependencies in a single configuration file, and logic and post-maintenance can be quite handy.

The use of the façade is mainly can not need to instantiate the class, you can use static methods to access the method of the class, it is more convenient to use, but it is also a disadvantage, such as can not directly jump to the corresponding method inside, also can not be intuitive to understand the use of this method, personal development may not affect much, However, if the team developed, in fact, it may be a bit dizzy to use it.

Take the Laravel file system as an example, in the providers array of the config/app.php configuration file, register a service provider:

Illuminate\filesystem\filesystemserviceprovider::class,

A façade is defined in the alias array:

' File ' = Illuminate\support\facades\file::class,

With these two steps, we can easily use the file system-related operations provided by Laravel, and the invocation form is very concise, such as:

    • File::exist($path)To determine if the file exists.

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

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

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

So how does this work? The following is a talk about Laravel service provider and façade mode.

Service Provider

First look at the definition:

The service provider is the hub where all Laravel applications start. Including your own application, and all Laravel core services, are initiated by the service provider.

In the file system of this service provider, location/vendor/laravel/framework/src/illuminate/filesystem/filesystemserviceprovider.php,register method, you can see that a single example is bound:

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

This singleton is a singleton pattern of the Filesystem class. Of course, this service provider can also bind other singleton, or do more things. Here we only study the File::exist() principle of this invocation mode.

Then there is a single case of files, which is actually an example of the Filesystem class.

At this point, if there is no facade, it is also possible to call the method to the Filesystem instance, and that is the invocation:

App (' Files ')->exist ($path)

Okay, now start talking about facade.

Facade Façade mode

First look at the introduction:

Facades/fəˈsäd/provides a "static" interface for the 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, and the facades provides more concise and rich syntax while providing more testability and extensibility than traditional static method calls.

At the beginning of this article, the alias array defines a File, and 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 the name of files, is not the name of the singleton pattern just bound? That's right.

In this way, you can use the File alias or façade to invoke the method in this Filesystem instance.

Through this article, I hope you can understand the relationship between the service provider, facade, and the instance of the class actually called.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.