In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the facade of laravel refers to. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
In laravel, the facade is a "static" interface for binding classes in the application service container. Maintenance provides easier-to-test, more flexible, concise and elegant syntax, with all facades defined under the "Illuminate\ Support\ Facades" namespace.
This article operating environment: Windows10 system, Laravel6 version, Dell G3 computer.
Laravel facade
Facades provides a "static" interface for binding classes in the application service container.
Laravel has a lot of built-in Facades, which can access most of the functions of Laravel.
As the "static proxy" of the underlying classes in the service container, the facade of Laravel can provide easier-to-test, more flexible, concise and elegant syntax than traditional static methods.
All facades of Laravel are defined under the Illuminate\ Support\ Facades namespace.
We can easily access the facade:
Use Illuminate\ Support\ Facades\ Cache;Route::get ('/ cache', function () {return Cache::get ('key');})
Throughout the Laravel document, many examples use facades to demonstrate the various features of the framework.
When to use Facades
Facade has many advantages, including a simple, easy-to-remember syntax that allows us to use the features provided by Laravel without having to remember long class names, and because of their unique use of PHP dynamic methods, they are easy to test.
There are some areas that need to be paid special attention to when using Facades.
The main risk of using Facades is that it will inflate the scope of the class.
Because Facades is very easy to use and does not require injection, it makes us inadvertently use a lot of Facades in a single class, resulting in the class getting larger and larger.
When using dependency injection, the more classes you use, the longer the constructor will be, which will attract visual attention, reminding you that the class is a bit large. Therefore, when using Facades, we should pay special attention to controlling the size of the class and keeping the scope of the class short.
When developing third-party extension packages that interact with Laravel, it is recommended that you choose to inject Laravel contracts rather than using classes in a Facades way. Because the expansion pack is built outside of Laravel itself, you cannot use Laravel Facades test helper functions.
Facades Vs. Dependency injection
One of the main advantages of dependency injection is the ability to switch the implementation of the injection class. This is useful when testing, because you can inject a mock or stub and assert various methods that are called in stub.
Usually, the real static method is impossible to be mock or stub. However, because Facades uses dynamic methods to proxy method calls to objects parsed from the service container, we can test Facades like an injected class instance. For example, a route like the following:
Use Illuminate\ Support\ Facades\ Cache; Route::get ('/ cache', function () {return Cache::get ('key');})
We can write tests like this to verify that the Cache::get method is called in the way we want it to:
Use Illuminate\ Support\ Facades\ Cache;/** * A test case for basic functionality. * * @ return void * / public function testBasicExample () {Cache::shouldReceive ('get')-> with (' key')-> andReturn ('value'); $this- > visit (' / cache')-> see ('value') } this is the end of the article on "what laravel facade refers to". 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.