Laravel Beginner tutorial | Laravel Facade - simplest explanation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the simplest explanation of facade so what is facade why we need facade and how to implement our own facade we are going to cover these three topics in this episode but what exactly facade is so it is just like a wrapper around non aesthetic function to of a static function this means you can call a non static function just like you call a static function if you are not getting this let me explain you so suppose we have a cache and we are going to set the cache with a name as sarthak and like this and then I am going to get cache get name so what about this is it going to work obviously it is going to work I am on web dot PHP file so if I go here refresh this page sarthak is here not cool and this shows that set is a non-static function but if you have little bit idea about the level or if you have used it you probably have seen that you can call the same function just like this and that time you need to use cache facade and this is not file system we need to use cache facade and that will be like this and this is going to work absolutely fine so if I go refresh this page yes exactly the same result so what exactly the type of this set and gate function is is it a static function or a non static function to have an idea about this let's go to cached repository so I will say cache repository and now here you can see at the line 227 I have a set which is a non-static function similarly you can search for get and you can see get again is a non-static function so how this the fact is going to call a non-static function just like a static function so that's so that happened because of facade so facade is facade looks like a simple class which is going to extend the base facade of level container or we can say core level not container and then it just have a one function protected static function called get the sad accessor where we are giving the facade name where we are giving the binding name so this is the facade and why we need facade because this implementation looks very easy and very clean and also you don't have to worry about how you can initialize any class and that's the pain point but by using society everything is taken care by level and the service container so we know what is facade non-static function to a static function why we need it because it's so clean and it can resolve with the dependencies and also it is very easy to test let's implement our own facade which is going to look similar to the facade which is we have seen here so for that for implementing our own facade I'm going to create a class called fish and this fish have a function called swim so return swimming another function called eat and here we are saying eating and how we can create the facade for it hmm that's a challenging part but let's see how we call this swim and eat it's very easy we need to say fish is equal to new fish and then we call fish and swim this is the normal way yes swimming because this fish class doesn't have any dependencies so it's look very clean but when we have lots of dependencies like news other class then that class also require new some other class then it will become painful so now what I am going to do I am going to bind this with our container so I will say whenever I call fish you need to give me the fish class so return new fish and you know what is container how to bind these things so let's create our own facade very easy let's say a new class called fish facade and Here I am going to use a magic function which is called call static so this will have two argument like name and arcs that means argument and it is a static function okay so first thing I am going to die dump name and if we will see how this is going to work so if I say fish facade and call any random function which is obviously not available and function it doesn't matter okay so what is the result because we have used this magic function whenever we are calling any static function which is not available on this class it will call this function and this function has this name which is actually the any random function literal so if I go refresh this page yeah any random functions there this is because we have diadem name here so if we want to say fish facade and swim like this so if i refresh we get the swim that means here we can access that function name it's now becoming very easy because now we can say return app me and we are calling or we are resolving this fish class from the container not directly because we are we want this to be implemented one time only if we have various dependencies so now we have resolved this fish and then we call that function on this fish class so look at this we are trying to access this fish class aesthetically but behind the scene we are calling this as an honest ethically so if I go refresh this page hmm it is not there because we need to die damn it so die damp refresh this page swimming is there yeah very cool this means now it is becoming general so we have two functions so we man eat we can simply call it function also so refresh and eating is there so whatever the number of functions you have you can easily call on this facade so this is the simplest explanation of facade but I told you that if we need to create a general facade which just look like this okay so let's create another class another class simple called bike bike has a simple function called start and I am going to return is starting hmm let's bind this also to the container so app bind whenever I am calling bind I will get new and that has to be returned new bike yeah this is again binded and if we want we can create a simple facade for bike facade also like this but I want to make general facade so that I don't have to write this static function called static magic function again and again so here I will create a class called facade and I will move this magic function from here to this class and now let's extend our base facade class now instead of this fish I am going to create a function here function like this and this function name is get the sad accessor and this time I am going to give a string which is going to result from the container so for the fish facade the accessor is fish similarly for this bike facade the accessor is bike and this accessor is actually the string we have binded to the service container so look at this it is looking just like we have cache facade protected static function gets assert so make it protected I think and protected static I want this to be same as this that's why I am making it protected static and that has to be static actually so this is looking very similar to this cache facade class now let's implement this so instead of hard-coding this I want to get the accessor name via get accessor so static get facade accessor and let's create a dummy function so that it can overwrite so get facade accessor and not going to implement anything okay so we have done this and this has to be again protected protected static so whenever we are calling like this facade Eid it will go to fish facade it will say okay each function is not there that means I need to go to call static magic function it will say ok resolve from the accessor which is fish in this case and then call that function this means if I go to here eating is there if I say swim swimming is there also if I try to access bike facade and I will say start and we are getting this error because we have not extended our base facade so extends facade and now if I go to fresh this page starting is there so this is how you can see it is very easy to create your own facade but lateral it has given you everything for creating the facade very easily so you can see our facade is looking similar to the lateral facade and this is how you can create your own facade I think you got this idea how facade is going to work so let's again recap everything I have a class called fish we have a function non-static function swimmin eat then I am binding that fish class to the container so that I don't have to initialize this fish class again and again then I have a by class which having a non-static function called start again I'm binding this bike class to the service container then I have a base facade class which is having a magic function called static magic function and a protected static function called get accessor get facade accessor get facade accessor is overwritten in every facade class we have created like fish facade is over writing this get facade accessor which is returning this fish similar bike facade is returning bike so on this base facade class we get the facade accessor and then we are resolving it from The Container app make means resolving we are fetching from the container and then calling the function we are trying to access so this is how you can call bike facade start and this start function is actually not inside facade not inside base facade it is inside a simple class and it's a non-static function so I think you got how facade created what is facade and if you have any question you can email me message me anywhere there's lots of platforms are there and we will meet in the next episode somewhere to learn more about level till then goodbye
Info
Channel: Bitfumes
Views: 25,604
Rating: undefined out of 5
Keywords: php framework tutorial for beginners, php framework laravel, laravel 2019 tutorial, laravel 5.8 tutorial, laravel 5.8 features, bitfumes laravel, laravel facades, laravel facades explained, facade laravel example, laravel facade tutorial, what is facade in laravel, explain facades in laravel, how laravel facade works, laravel facade simple explanation, laravel 5.8 facade, facade in laravel
Id: zD2VJhOdI5c
Channel Id: undefined
Length: 13min 49sec (829 seconds)
Published: Wed Jun 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.