Laravel 6 Advanced - e4 - Facades

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back so we're gonna be tackling facades now if you're not familiar with the actual term of facades I bet you if you've been working with laravel long enough you've definitely used a facade and arguably facades are probably the most discussed feature of levo and I do feel personally that it comes from the fact that people don't quite understand how facades actually work now at first glance a facade looks like just a static method call but behind the scenes it has nothing to do with static methods except the fact that that's just what it looks like so as I was prepping for this lesson I figured what would be the best way to explain facades and what I came up with is one oh we build our very own implementation of facades based around how laravel actually does it and that way we can take a look at how to actually implement a facade and then to wrap it up we'll go ahead and explore the actual implementation that level uses behind the scenes for facades and you'll see how similar they are to our actual implementation that way you're going to have a very firm grasp on facades and how they actually work now using facades is extremely easy understanding how they work under the hood that is an advanced level topic so let's jump right to it let's go to phpstorm and this is a totally fresh level six project I don't have anything in here yet so the example that we're going to be working with today would be the example of a service it doesn't really exist I made it up but what it's going to be it's it's going to be a postcard sending service just a very simple way for somebody to send a quick postcard by the mail to somebody else very simple stuff we're not really going to implement it this is just an idea so we can understand how we would actually work so I will actually do most of the actual work in our route files but of course this would be something you'd have any controller or something like that so let's add a new route here and this get route will simply be called slash postcards all right so let's have a call back right here and of course we're going to need some sort of postcard sending service again it doesn't exist so let's go ahead and make it up so inside my app let me add a new PHP class and it's going to be called postcard sending service alright so this very simple class here obviously let's have a postcard service and this is going to equal to a new postcard service and what sort of information do we need for the postcard well what I was thinking was maybe the first argument would be country the second one would maybe be the width of the postcard and maybe the height again all made up figures none of this actually exists alright so let's jump right here let's add a constructor and my constructor is going to have the country where we're sending it to the width of the card and the height of the card so let's go ahead and initialize these fields and there we go so we have the setup of it done now let's do the actual grunt work of it which will happen in a method I'll simply just call hello so hello will accept our message the message that we want to send to our person in our postcard and finally let's just have maybe an email so in here what's the easiest implementation we can do why don't we just send them an email so we'll start off with that we'll send a raw email containing the message and then as a call back we'll actually need to set our email address now because PHP doesn't actually have access to email we'll have to go ahead and use email so in here I could just say message this message is going to be to this email address all right then here we'll actually mail out somehow our postcard through some service and of course at this point you'll have access to things like this country and this width and finally of course this height so these are things that are going to be set up through the constructor of our service and of course we'll use these so somehow call out a service that actually sends out a postcard all right simple enough so for this example we'll go ahead and dump at this stage and just say all right well the postcard was sent with the message and then we'll just tag on the message and we won't have to add a period there all right so very simple class it doesn't really do anything again this is just for an example so we have this postcard sending service we're going to construct our method and then let's go ahead and use it right here so of course country we're gonna have to pass in maybe us as a width will say it's 4 inches and for a height maybe we'll say is 6 inches so we're gonna send a postcard to a u.s. address with a width and a height awesome all right so what will we have to do after this well of course we'll have to actually call that hello method that we implemented and we'll say hello from coders tape USA all right and as an email it's going to be test at test comm and there we go all right let's give this a go in our browser let me jump to Chrome will visit facades dot test slash postcards and it says that mail was not found that's because I did not import mail let me go ahead and do that now so mail I do need to import this class and there it is and as a matter of fact funny enough façades this is actually a facade this is what it looks like when you use one notice how raw seems to be a static method but we're getting ahead of ourselves let's not worry about that just yet let me go ahead and refresh here and there we go so we did everything correctly so of course we actually sent out a postcard but the thing about this is that if I wanted to send a postcard somewhere else in my application this setup will basically need to be done exactly the same way so of course in the first episode of the series we covered how to bind that into the container and then not have to worry about those things and facades are loosely based around that so as you may have been familiar with from the app service provider we can go ahead and in my booth method I can say alright this app register a singleton and the singleton that we're going to register would be postcard service class and how do we construct one well to do that will basically have our app here and we already wrote the logic for this so all I would have to do is just copy it from right here so this is how we construct that so we'll return it as an object and there we go so at this point this would work so in our controller we could go ahead and just simply ask for this class and it would be created for us but we are discussing facades so let's go ahead and create a new route here and this is going to be version two this is the facade version of this implementation so we'll use facades and then function alright and inside of here the first thing I'll need to do as you know with facades is you'll actually need a file so let's create a fresh PHP class and let's call this one postcard the first thing I need is an alias right the way that these work is with an alias now from an app service provider standpoint we're passing in a class name but you may not know this you can actually just pass in an alias so we could say you know what is just a string for postcard okay so now that we have this class created to understand facades where the magic actually happens is a PHP feature called magic methods now you may not know this but you can have magic methods that grab any calls to any static method that doesn't actually exist on the class and the way that it works is that you would have public static function and the magic method needs to be called call static so if you have this method in any of your classes what's going to end up happening is that if you call a method statically on a class in this instance postcard then if it trickles down then of course it will answer this and I'm gonna prove it to you right now it's a very simple experiment so we'll say alright postcard and we are importing that up here at the top right so postcard let's call a method that doesn't exist we'll just call it any and what do you expect to happen let's go ahead and go to slash facades it says call static must have exactly two arguments but that error is actually coming from the fact that this is here I'll comment that out all right let's try it again there we go so we're calling to an undefined method of any it doesn't exist so how would you typically fix this while you add a public static function called any and maybe let's just dump in here and just say inside so now if we refresh of course we get inside but check this out if this method exists then it will call it but if it doesn't exist then it will actually call call static now call static receives two methods the first one is going to be the name and the second one is going to be any arguments that you pass in will show what each of these are so let me go ahead and move to dump statement inside of call static and if I hit refresh I still get it and to prove it let's do one two three so there we go so even though any doesn't exist inside the postcard as a static method we are able to grab that call using this magic method of underscore underscore call static all right so now that we have that let's go ahead and dump name because I want to show you what that actually gives us so we'll die and dump the name and we'll hit refresh and any so obviously name is actually the method that we're trying to access so in this case it's called any if it's something and we'll come back here it will be something so let's rename that variable so it makes a little bit more sense and let's just call it method so that's the method that we are calling and then arguments let's go ahead and dump arguments and now it's gonna be empty but let's go ahead and add an argument so it's anything that I pass here so we'll say 1 2 3 and then maybe ABC as a second string if I hit refresh then we get an array of each of the arguments so we have one two three and ABC are you with me so far obviously this is just a PHP feature this has nothing to do with level but it's just a language feature that we have so now that we've captured this static call we can actually grab the method and argument and resolve the facade this is exactly how level does it so what we could do is the following you may not know this but you could use the app helper and the app helper will actually give you your entire app now I know it doesn't look like a lot right now but this is your entire level application but you can actually access any particular bindings from your app service provider or any other provider and the binding that we're looking for right now is the one called postcard but you can access any of the bindings so check this out in our app as an array we can actually call postcard and now if we hit refresh notice that we get back a postcard sending service so we are diving into the container and we are grabbing our singleton and what we're actually getting is our postcard sending service pretty cool stuff right so it almost works like magic you can also do this a different way I'll just show you it's the exact same thing but of course there are different ways that you can do this so another way is that you could use the make method and this will give you the same exact thing I'll show it to you we'll hit refresh and sure enough you get the exact same result so you can access it either way we'll stick to the array notation so app again is your entire application and then you can access any bindings using the array notation so okay so let's go ahead and add this postcard maybe somewhere here in a different method so let's have a protected static function and let's call it resolve facade and result facade is going to accept a name and inside of here this is sort of where we're going to do this right here so we'll go ahead and return our app and we're gonna dive into our app and I'm gonna say go ahead and resolve anything with this name whatever the binding is at this point we'll go ahead and resolve that and return it right away back to the user so now when we call this statically the first thing we need to do of course if we need to resolve the actual facade out of our container so to do that we can simply return and say okay self remember we are in static mode right here so you cannot do this at this stage because there is no this we are calling this statically so we need to call everything from a static perspective so to call it from a static perspective you use self : : meaning its self go ahead and call oneself so let's go ahead and resolve a facade and the facade that we're looking for is postcard so now that we have that let me go ahead and just die and dump this because I just want you to see what we get each step of the way so let's go ahead and resolve this and sure enough we still get our postcard sending service so at this point we have our postcard and so all we need to do is grab our method now let me refresh your memory one more time with this method I'll go ahead and die and dump method and remember method is at this point something let's go back and change it to what we actually want remember we're trying to call this hello method so let's call the hello method so now that is equal to hello great so now what we could do is on this object that we got back we can actually call in the method and this method call is actually dynamic notice the dollar sign in front if it was without that then it's just simply a method call but it is static we're physically calling method but if we add the dollar sign now we are dynamically calling method but it's just a property unless you put the actual parentheses and now it is a method call now you may not know this but from our arguments right here we can simply spread anything in that array using three dots so we'll say dot dot dot and then arguments and that's it at this point this is a working implementation of a facade let me prove it to you I'll simply hit refresh and now it says that the address in the mailbox giving ABC does not comply of course we are calling some random thing and it's not really working with ABC but if we say hello from Assad and then let's give it an email ABC at one two three dot-com let's give that one more chance and there we go so postcard was sent with the message of hello from facade so notice the difference this is the long way of doing it and this is the facade way of doing it so the one advantage here that you can see right away is the fact that we don't have to worry about instantiating this class and passing all these different methods to it but rather we're just simply calling this nice postcard method and not worrying about it at all so why don't we dive deep and see how the level implementation is different from the one that we just created but before we do that let's do a very quick recap of what we actually did so in my apps service provider I registered a singleton and the singleton is going to be reference using postcard it's just simply a string so what we are actually outputting out of this callback is the instructions on how to generate this postcard sending service it doesn't really exist again but this postcard sending service all it does is it really it sends an email and then it's supposed to be sending some sort of postcard through a service and then we're simply dumping out the message that we included that's it very simple class it doesn't really do anything so then in my actual postcard this is our implementation of a facade so what we're actually grabbing is we are grabbing our entire application and we are resolving out name so in this particular instance name we are setting that equal to postcard to match the exact same string that we have inside our app service provider so inside of this postcard we are actually calling a dynamic method on it that we receive through this call static now call static is a PHP function that again grabs any static method calls that get passed into this class when the class actually doesn't have a static method by that name and the static method that we are calling is hello so we are calling hello but hello doesn't exist on this postcard class there is no method here actually called hello the hello method is inside my postcard sending service but notice that this hello is not static this is a real public method inside a real class that does get completely instantiated and it is not a static call it just makes it seem this way because of the notation but the way that we've written the code around it this is a real class this is no different than this implementation up here these two lines here and this one line here do exactly the same thing the only difference is that this class has a nice name nice alias and we can simply not worry about having to instantiate the class the way we did up here we just simply call it in this nice notation and we are good to go all right so with that recap under our belts let's go ahead and dive into the level version so now there is a file inside your project that you may not even know exists but inside bootstrap inside cache we have this services dot PHP class and if we scroll down a little bit out of this class we have this list right here so check this out these are all basically the same bindings they are aliases no different than this alias right here that we generated so let's take a look at the mail facade so the mail facade is actually called mailer and I'll show you how to actually look for it so if I go back to postcard sending service here's the facade that we are using and here's the class form so we can actually jump into this class and you notice that this class of mail extends facade we'll take a look at facade in just a second now this first method here has to do with testing and how you can swap out a facade for a fake forget about this method for now we are not interested but check out this right here this is the get facade accessor and notice what we have here this is very similar to our implementation in our implementation of course it was postcard and if you remember from our actual facade it's right here so we have this method right here that all it does is it just returns the string of mailer and the string of mailer is matched up to this mailer right here so this is the same thing this is what it's referencing now the one thing about it is that you can see that it is associated to the mail service provider so let's jump to the mail service provider and let's check out what needs to happen here now notice this right here we are registering a singleton exactly how we did in our app service provider right here now the difference is of course we have one line of setup but check out how big the setup is for instantiating a mail facade it would be very difficult for you to have to repeat all this code every time you needed to instantiate mail so mailer is setting up a nice singleton inside of our container and of course it is actually generating a new mailer class which is this mailer class would be equivalent to our postcard sending service class which is a real class but if you keep going inside of here of course you see things like setting up for queues we see things like setting the from the reply to this is a very complicated set up and if we jump into the mailer class you'll notice methods in here like two for example but notice that two is not static you'll notice CC for carbon-copy and blind CC and HTML and of course raw this raw class if you remember back from our post cut sending service is right here but notice how again it looks like we are calling raw statically but we are not the raw method as you can see right here is a real function and again it's called raw and that is what we're actually calling we're just calling it through the facade so with that let's take a look at the actual facade so if we jump back into the postcard sending service remember we are using the mail facade and if I jump into mail remember that it extends facade let's take a look at facade of course there are many methods in here and this is what they all are but the one that we are interested in is the get facade access err if you remember this is the method that we are overriding this is the one that said mailer so now if we scroll down check this out does this look familiar right here yeah I think it should that's exactly what we did in our facade notice it right here so we are grabbing the app and again using the array syntax we are resolving it out of the container if we take a look back in the facade there it is we are statically grabbing the app and again grabbing and resolving the name now it does do it a little bit more fancy because there are different cases that it actually handles but this is ultimately the same thing that we did and then furthermore if we scroll all the way to the bottom check out this call static method again very very similar to ours with of course the only exception being that levo does a little bit of error checking and things like that but ultimately this right here is what we're looking for so we are actually grabbing our instance which we resolved the same exact way that we did and then we're calling this method dynamically and we're just simply passing in any arguments so that is a deep dive into how facades actually work and again this is one of the most misunderstood things at a level but now armed with the information that I've provided for you in this lesson you have a firm understanding of how facades actually work under the hood so that's gonna complete our lesson today on facades as always don't forget to subscribe share the channel my name is Victor thanks for watching
Info
Channel: Coder's Tape
Views: 47,647
Rating: undefined out of 5
Keywords: laravel 6, laravel tutorial, laravel 6 tutorial, laravel 6 what's new, laravel, laravel service container, laravel ioc container, service container, service container laravel, container laravel, laravel container, laravel ioc service container, laravel advanced, laravel facades, laravel facades explained, facade laravel example, laravel facade tutorial, what is facade in laravel, explain facades in laravel, how laravel facade works, facade in laravel, Facade Pattern
Id: zR6JnwH7MSQ
Channel Id: undefined
Length: 24min 33sec (1473 seconds)
Published: Thu Aug 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.