Laravel Facades: What are Facades used in Laravel & how to use them in App with examples?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
laravel facade is one of the most discussed topic and people have different opinion about facades some people like it and some don't i also want to hear your thoughts about facades so please share what you think about facade in the comment section in this video i am going to show you what are facades and how you can create your facades and we are also going to implement our own implementation of facade to understand how facades work in the laravel code base so let's start [Music] [Music] hello everyone welcome to our channel my name is harish kumar today's topic is laravel facade according to the documentation facade provides a static interface to classes that are available in the applications service container what is the meaning of that let me show you so let's go to the editor here i have already created a fresh laravel application let's say here we have some services so in the app i will create a new service class so let's say in the services directory we have some service example dot php and let's start php namespace is app services and and let's say this class has a method do something now here let's say just return done now let's go to routes web.php here let's remove this command now here to execute this method what we will generally do first let's initialize dollar service is equal to new some service example class and now here we can say dollar service do something like this let's dump this let's go to browser so first php artisan serve and let's open this url this do method is a regular method so can you execute this statically like a static method so you don't have to initialize this class let's see so i'm going to comment it here and some service class and then i am going to call this do something like this now what will happen if you go to browser and refresh and we will get added non-static method do something cannot be called statically because it is not a static method and that is what facade does a facade is a static interface for a class it allows to call non-static method statically let me show you an example of in build facade so let's remove this and let's say here we have config facade this one illuminate support facade config and you can also see that illuminate support facades route here we are calling get method statically let's see config facade as well so here we can call config and then we can call static method get like this then we can pass the key for configuration that we want to get let's say app dot name let's dump this let's go to browser refresh and here we get laravel and if we see this config facade class this config facade has only one static method that is cat facade accessor and it extends facade class let's see this and here in this class we cannot see a get method that we have called statically right here so from where this get method was executed and here we can see in the doc block we have get method and if we see this config repository class right here we should have get method and that is the method which was called statically right here and you can see it is a non-static method and now you understand what is facade it provides a static interface for the class and remember that this facade has created the instance of config repository class from the service container if you do not know about service container then you should watch my previous video on service container in that video i have explained service container in detail basically service containers help us manage application dependencies and in this facade you can see here we have a static method which returns a string config that is the key which has stored the instance of this class in this service container let me show you so here if i dump this app and this app has stored the all applications dependencies and in this app helper i will pass this config key now go to browser refresh and you can see here we get instance of eliminate config repository class from the service container now let's create a facade for this some service example class so we can call do something method statically so our first step is to register this class in the service container so let's go to app service provider and in the register method here i'll say download this app bind and then i'll provide bind key let's say some service next i'll pass a closure function and it is going to return new some service example class is done now let's create a some service example facade so here some service example facade dot php let's copy all of this content and paste it here and its name is some service example facade and this some service example facade should extend facade this one eliminate support facades facade and this this facade should have a static method protected static function get facade accessor and that should return a key that we have used in this service container binding some service like this now let's go to web.php right here now we can call this do something method statically let's see so here i will call some service example facade now call do something let's go to browser refresh and it worked it has executed do something method from the some service example which is non-static method but we have called it statically how cool is that so what here we did first we bind the class in the service container and then we created some service example facade that extends eliminate support facade facade from the laravel application and this facade should have a static method get facade accessor and it should return a key that we have used to bind class in the service container this one now it is not necessary that you have to bind a class in the service container to create a facade so if i comment this and refresh of course we will get error because this key does not exist in the service container so in the facade here i will return some service example class it is going to work that is because if you have watched the previous lesson on service container then you know the service container has a feature auto resolving dependency so from this get facade accessor if i return this some service example class service container going to auto resolve this dependency now so let's see let's go to browser refresh and it worked we don't have to create binding for this class now what if this some service example has a dependency as well so let's create a construct method public function construct and this construct has a dependency let's say string dollar secret key protected dollar secret key [Music] download this secret key is equal to now this time let's refresh and it will fail because laravel does not know how to resolve this string secret key in that case we need to register this binding so let's uncomment this binding and here this some service example need a dependency of secret key so let's pass some random string like this and next in this some example facade here we should return this some service key now let's go to browser refresh and it works and this facade has saved these two line of code and it looks very clean and easy facades is very useful but we have one issue the text editor is not smart enough to detect all the methods available to us in the original service class so there won't be any method autocomplete when we use facade for that in the facade class we can insert php documentation like this now here we should see autocomplete here we go and you may have also seen that for config get app.name let's dump this for this config you do not need full class name we can just say use config and it will work here my text editor has not recognized this class but in the browser it should work refresh and here we get laravel how it has worked if we see config app.php let's scroll down here we have all service provider and here is the alias and you can see here we have alias for config which is which is alias4 eliminate support facades config class because of this aliases we do not need to use full class name we can just use config and it will work similar like that we can also add a alias for this some service example facade let's add this so in the config app.php let's scroll down and right here let's say our alias name is some service which is going to be alias for app services some service example facade class now in the web.php we can call some service and then we can call do something method statically like this and we do not need to use this facade we can just say use some service like this now let's go to browser refresh we get this laravel that is from this config and here we go we get done from this do something method next let's create a our own implementation of facade to understand how facades are working in laravel so let's close all these tabs and in the app here i'll create a new file let's say this is facade dot php namespace is app next class is facade and this facade class going to have a public static function call static this call static method is a php magic method and it automatically calls every time if we execute a static method that does not exist in this class for example in the web.php let's comment this all this and here if i dump facade let's see any static method which does not exist and make sure to import this facade and right now this any method does not exist in this class so it will automatically call this call static method first let's remove call static to make sure it gives an error call to undefined method any now if i add this call static and here i am going to dump this dollar name refresh and you can see this the method name this one if i say something else and now it should be something so let's say it is method name and it is going to catch all the arguments we passed in this method let's say one two three and it should see array of one two three now to implement our own facade here in this call static i'm going to resolve the class from the container for example let's dump app and then key for example let's say config which is going to dump the config repository class from the service container refresh here we go similarly like from the app service provider we have registered this some service some service and it will give some service example class that we have bind in the service provider so here what i will do is say dollar instance is equal to app and in this app method i will pass static get facade accessor and it will give the instance from the service container and then we can say return instance and we can call this method dynamically like this and we can pass these arguments using spread operators and next let's create this get facade accessor right here protected static function get sort accessor like this here we can throw exception for get for sword accessor override for now let's leave it empty and that is all our facade is created so next in the some service example facade now here i'm going to extend this some service example facade with our own custom facade implementation so let's comment this and here i'll say use app facade and in the web.php here we can say some service example facade do something and it should work and here we go it is working our custom facade is working now let's see the laravel's implementation of facade so let's go to this eliminate support facade class here in this facade class we can also see that it has call static method similar like our custom facade and here you can also see that it is getting the instance from the get facade root which is also behind the scene getting the instance of a class from the service container and is doing extra check if this instance does not exist and then throw an exception otherwise execute this method same as we did right here lara will also provide a very cool feature that is real time facade it is even more easy to use using this feature you may use any class of your application as if it is a facade let's see an example of it so let's say you have a class example.php namespace app and class example and it has method let's say process and it just returns processing now let's go to web.php now right here let's call this process method statically so example and then process like this of course if we go to browser it will give error because we are calling non-static method statically so to implement real time facade i will add a word for search to the beginning of this namespace like this facades and that's all we need and it has worked we have not created any facade for this example class we have just added facades word in the beginning of this namespace and that is how it is simple to create real time facade behind the scene what laravel does it creates a facade for this class in the storage framework cache and here is the facade class example and in the get facade accessor it returns the class with its namespace how cool is that this is the end of this lesson i hope it was easy to understand how facade works in laravel if you like the video hit the like button share this video and don't forget to subscribe us see you in the next lesson [Music] you
Info
Channel: QiroLab
Views: 6,742
Rating: undefined out of 5
Keywords: facades, Laravel Facades, Real-Time Facades, Facades Vs. Dependency Injection, When To Use Facades, Laravel Service Container, IOC Service Container, Dependency injection, learn with qirolab, qirolab
Id: qSJlovwPEJE
Channel Id: undefined
Length: 21min 19sec (1279 seconds)
Published: Tue Feb 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.