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.

Share:
Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

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

Most Popular Posts

An Introduction to WordPress Action Hooks

Posted on 5 years ago

Bhumi

How to use HTML5 AutoFocus attribute

Posted on 2 years ago

Bhumi

Facade implementation in Laravel

Updated 1 month ago

Bhumi

MySQL coalesce() function

Updated 18 years ago

Bhumi

How to use JavaScript Promise API?

Updated 18 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 18 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi