In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is the role of laravel facade", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the role of laravel facade" this article.
In laravel, the facade Facades is used to provide a static interface for the classes of the application's IoC service container. The facade of Laravel acts as the static proxy of the underlying classes in the service container. Compared with the traditional static methods, the facade of Laravel can provide more easily tested syntax during maintenance.
The operating environment of this tutorial: windows10 system, Laravel6 version, DELL G3 computer.
What's the use of laravel facade?
Introduction
Facades provides a static interface to the classes of the application's IoC service container. Laravel comes with some Facades, such as Cache, etc. 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.
explain
In the context of Laravel applications, a Facade is a class that allows access to an object from the container. This function is defined in the Facade class. Laravel's Facades and any Facades you define will inherit the Facade class.
Your Facade class only needs to implement one method: getFacadeAccessor. To resolve anything in the container, it is done in this method. Facade, the base class, uses the _ _ callStatic () magic method, which can be deferred to the call from Facade on the resolved object.
So, when you use Facade calls, such as this: Cache:get,laravel will resolves the cache management class from the Ioc service container, and then call the get method above the class. Laravel's Facades can locate the service, which is a more convenient syntax for using Laravel's Ioc service container.
Advantages
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.
Practical use
The following example invokes Laravel's cache system. If you take a look at the following line of code, you might think that this is a direct call to a static method called get above the Cache class.
$value = Cache::get ('key')
However, if you look at the class Illuminate\ Support\ Facades\ Cache, you will find that there is no static method get at all:
Class Cache extends Facade {/ * * Get the registered name of the component. * * @ return string * / protected static function getFacadeAccessor () {return 'cache';}}
The Cache class inherits the base class Facade, which defines a method called getFacadeAccessor (). Notice that what this method does is to return the name of an Ioc binding, which is cache.
When the user references any static method on the Cache Facade, Laravel will resolves cache the binding from the Ioc service container and execute the requested method on the object (in this case, the get method).
So, when we call Cache::get, what it really means is this:
$value = $app- > make ('cache')-> get (' key')
Import Facades
Note that when using facade, if a namespace is used in the controller, you need to import the Facade class into this namespace. All Facades are under the global namespace:
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.