Home > PHP Framework > Laravel > body text

Usage of service provider and facade pattern (Facade) in Laravel framework (code)

不言
Release: 2018-07-31 18:00:37
Original
2647 people have browsed it

laravel framework It is very convenient to use the related operations of the built-in file system, so how is this implemented? In the next article, php Chinese website will give you a detailed explanation of the principles involved. Without further ado, let’s look directly at the content of the article.

Let’s start with an introduction:

Take the file system that comes with Laravel as an example. In the providers array of the configuration file in config/app.php, Registered a service provider:

IlluminateFilesystemFilesystemServiceProvider::class,
Copy after login

Defined a facade in the alias array:

‘File’ => IlluminateSupportFacadesFile::class,
Copy after login

Through these two steps, we can very conveniently use the file system related information provided by Laravel operation, and the calling form is very simple, such as:

File::exist($path) to determine whether the file exists.

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

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

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

Let’s talk about Laravel’s service provider and facade mode respectively.

Service provider

Let’s take a look at the definition first:

Service provider is all Laravel The central location where applications are launched. 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

Facades provide a "static" interface for classes available in the application's service container. Laravel comes with many facades, which can be used to access almost all of its services. Laravel facades They are the "static proxies" of the base classes in the service container. Compared with traditional static method calls, facades not only provide a simpler and richer syntax, but also have better testability and scalability.

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

IlluminateSupportFacadesFile::class,

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 this name files is not the name of the singleton mode 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.

The above is the entire content of this article. For more laravel content, please pay attention to the laravel framework introductory tutorial.

Recommended related articles:

Auxiliary functions in the Laravel framework: Introduction to the optional () function

Laravel5 .5 New feature: Analysis of preset command

Related course recommendations:

Recommended five latest Laravel video tutorials in 2017

The above is the detailed content of Usage of service provider and facade pattern (Facade) in Laravel framework (code). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!