Back to Top

Facade implementation in Laravel

Laravel Facade implementation

Laravel comes with very interesting features and one of the interesting features is Facade.A facade is design pattern in which some of the principles of its operation. Laravel comes with some Facades, In fact, you should have used them, but may not know what they are. Facades in Laravel are static agents like the IOC container class, which are simpler, easier to express, more testability and flexibility than traditional static methods.

What is Facade in Laravel?

A facade is actually a static proxy in a container or we can say it is a static way to call any object stored in the container anyway.

For Example,

By default, the cache class does not have a get static method but you can use this with facade function. To operate the Cache get method, you must first Cache the class. So this class is in the app / config / app.php You can see that there is an alias array, which defines some of the framework comes with the class alias. You can see Cache class call in that you can find Illuminate \ Support \ Facades \ Cache class.

How you can use Facade in Laravel?

If you customize a class and put it in a container, you can call Facade in very simple way:

In above example, You just need to define a class to inherit the base class Illuminate\Support\Facades\Facade, and implement an abstract method getFacadeAccessor.

This method simply returns a string that gives the alias of the service container binding class. In fact, through the source can know that the object does not have to be placed in the container, you can return in the following way:

How to achieve the Facade?

Let’s see the facade source code to understand Facade in base class.

This is core part of Facade in Laravel but there is more than that of the code, Let’s take a look at –callStatic method:

This is a magic method in PHP which is called when a nonexistent method is called in a static way. The code is very simple to resolve the instance call method but here to note that one is the use of static keywords rather than self keywords, which involves a late static binding.

Look at the code of getFacadeRoot method and is from the container to resolve the object:

Here, as per above code getFacadeAccessor method must be rewritten otherwise it will throw an exception. And then in resolveFacadeInstance method will first determine whether it is an object, if it is then directly return. So the above getFacadeAccessor method directly return to an object is also possible.

Then will determine whether the object has been resolved, if the analysis of the direct return, or from the container to resolve and then return, so that not only to achieve a single case, but also can improve performance. Get the object, that is, directly through the object to call the method:

Conclusion

Facade design pattern in laravel is not difficult but documentation of facade method is not clear. There are lot of facades available in laravel. Here I have explained cache one. But you can use any facade pattern.

Comments (2)

  1. Waiting for more blogs on laravel.

    1. Yeah, I will write more about laravel. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

An Introduction to Kubernetes

Posted on 8 years ago

Bhumi

How to use CSS3 Object-fit

Posted on 9 years ago

Bhumi

Fine-tuning ChatGPT with Lang-Chain

Posted on 1 year ago

Bhumi