Laravel 6 Advanced - e5 - Macros

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back up next we're gonna be tackling the macro Bowl classes in laravel now a macro is a simple way for you to add your own functionality to the built-in classes in laravel now a lot of times you'll extend the class if you have to do a lot of work but macros allow you to tap into these classes and add your own functions to them now there are quite a few of them so I've actually written a nice blog post that you can visit so you can reference and figure out what classes are macro Bowl and here is the entire list so as you can tell there are quite a few of these classes that you can add your own functionality to it now I don't expect you to understand exactly what I'm talking about just yet so let's jump right into the code and figure it out so let's go to phpstorm and what I want to do is pick a class that is macro Bowl and then let's do a quick dive through the source code figure out how it works and then figure out how to add our own macros to one of these classes so let's pick the collection class let's go to the collection class that's gonna be under eliminate support collection and if you look right up here it is using this Mac herbal trait so what exactly is a trait when you see this use statement up here which is obviously different than this use statement up here what it is is that we are adding additional functions that don't exist in this class and that allows you to reuse these exact same functions in different classes so the macro Bowl trait whenever there's a class that is actually using the macro Bowl trait it inherits two methods that are important to us and if we go to this macro bowl trait you'll see that what we have is this static function called macro and the second one is this mixin of macro now we will talk about both of those but the very quick explanation is that this allows you to add one macro at a time where a mixin allows you to pass in an entire class worth of macros so if you have more than one this is probably the best way to do it you see things like has macro this is what you would use if you wanted to check if the macro that you're trying to use is available now we are not really gonna dive into this but here is the very important part and this is the backbone of macros so you may be wondering well how can I add my very own classes well here it is whenever any static function is called on the particular class that is using this macro bolt rates of course as we learned in the previous lesson about facades anything that doesn't exist will fall back to this magic method of PHP this is just a PHP language feature that we have so called static is any static methods that don't actually exist on the clasp so the very first thing that it will do in this if statement is well thus this class have a macro by the name of method meaning the very first thing that got sent in and if it doesn't then of course it's gonna say you know what I don't have anything for you because in this case we just don't have a method that matches that name so if we get past this initial gate check then we go ahead and set the macro equal to the macro that we're looking for so the way that it works let me jump back up here whenever you register a macro the only thing we're doing is adding to this macros array so initially the macros array is totally empty so every time we call this macros function we are just appending to this array whatever the macro name is so down here all we are setting is fetching the correct macro from that array and setting it to this variable of macro so then if the macro is instance of closure meaning it's a function it's a callback it's something that we can actually call well in that case and we're actually going to invoke that function and we're going to return whatever the result is and then we fall back to this last step which is we actually just invoke the macro and then you see that we pass in any other parameters that we can actually have so we have a couple of different ways that a macro can be called so that's a very quick and dirty of how macros work so how about an in action example so let me go to my app service provider and of course you can do this in your very own provider or even in a package this is very useful for packages so right in my boot method let's go ahead and add a macro for the SDR or the string helpers so the way that you would declare a macro is first and foremost of course we're gonna use STR that's aluminate support STR it's a class in level which of course has a bunch of string helpers and if you actually check out the helpers you see that there are quite a bit of them but for the sake of this lesson I'm gonna make up a brand new one that doesn't exist so to do that to add the macro remember we have that macro function this one right here so if we call that macro on STR now before we even go on let me take a quick pause and let me show you something inside STR if I navigate to illuminate support STR notice here that it is using macro both exactly the same way that this collection class was also using macro Bowl and it's using the exact same one so as a matter of fact in your very own classes you can even use this macro bowl trait if you wanted to just have to make sure that you import it like so at the top all right that was just a quick side note of STR so now we can actually add our macro now it's going to take two parameters in here so the first one is going to be the macro name so I will make one for part number and the second one of course is going to be our actual callback that we have to actually generate this macro so remember whenever we call STR part number we're going to invoke this function right here now if we are going to pass in any parameters or arguments into part number we can accept all of those right inside this function call so I will actually accept a part as a part number and this is just going to format a part number for us it doesn't really exist I'm making this up as I go but let's just say that for the sake of our program it needs to return something like a B - and then maybe the first 3 digits of whatever part passed in so we can simply do that with sub STR meaning give me a subset of the current string so as a first argument we'll pass in our string we'll start at 0 and end at character 3 and then after that let's go ahead and add another - and then finally let's get the rest of our string so we can do that using sub STR as well but in this case we're not gonna pass in a third parameter we're just gonna say go ahead and start at 3 and give me the rest of them does that make sense this is just a very simple string helper function that our fictitious application is going to have so we're gonna pass in a part number of some sort in some sort of format and I want to get back a B - then the first 3 characters and then another - and then the rest of the characters alright so as a playground let me jump into the route file and let's add some stuff here that we can play with so i'll just actually just die and dump and I will use STR again that's illuminate support STR and now we have access to this new part number remember this doesn't really exist in real level I added this on the fly so now as a part number I will just simply type a bunch of random numbers and now when I head back to the browser I'm gonna hit refresh on this and check that out so we are currently formatting that string so we've just added our very own function into a level function and that's what macros are really good about it allows you to inject your very own stuff into the actual stuff that ships with laravel how cool is that very cool stuff very useful so again if we actually disable this and we head back in here of course that doesn't exist so we get this new bad method call there's no such thing as a part number it doesn't exist so of course once we turn that back on we hit refresh there we go so now we're working so now our entire application has access to this new part number function globally as an entire thing so pretty cool stuff what else could we use these macros for well there's another one very useful that I use sometimes which is the response Factory now of course out of any controller you can always have some sort of error so it would be nice to maybe have a premade error response now you can just call and say you know what something happened here's my error message and I'm done with this request so how about if we worked on an example for that so let's add a new macro for the response factory so the response factory's what actually gets called whenever we send out a response it generates a response as the name suggests it is the factory of responses and of course right away we see that we have access to this macro method exactly the same method let me show you one more time just to drive the point even further I'm gonna jump into this response Factory and again notice how it says it uses macro but again just to reiterate I've actually included the entire list of every single class that is macro Bowl and of course we do see the response factory being one of those so any of these classes that you see here are macro bow of course we used STR and that one is down here so just keep that in mind keep this blog as a reference so you know where you can add your very own classes which is to be honest and just about everywhere in level we seem to have added macro Bowl to just about the entire framework so now in this macro let's create a new one for error JSON again it's gonna be a made-up response that I want to return every time I get an error so in here we'll have a function and this one's going to be a simple one I'm just simply going to return an array and this is the formatting that I'm going to use I'm gonna say message I'll have some sort of default error message here and of course if I wanted to I could pass in my message right through here so we could say maybe message and then let's actually default error message right here and then let's go ahead and just use message there we go so now it's customizable pretty easy pretty cool trick and then of course let's have some sort of error code doesn't really exist and it's gonna be just error code one two three all right so how do I use this response Factory head back over here and I'll actually comment that one out and let's make a new return statement here and I want to return and say you know what I want out of this I'm not interested anymore so I can say response that's gonna be the facade response so eliminate support facades and I can simply just call error JSON alright let's give this a go let's see what we get refresh and there we go so we have our default error message with error code one two three now again if I wanted to customize that message I can say a huge error occurred boom and sure enough a huge error occurred this allows you to really encapsulate some of the logic that is application-specific for you without having to extract something like a service class and sometimes we jump too quick into extracting things into service classes macros are a good alternative for something like this so alright so let's take it to the next step what if I had a bunch of macros that I wanted to declare all at once now as you can imagine if I kept declaring them over and over in this boot method it's gonna get kind and large so why don't we create a dedicated class just for macros so those are called mix-ins and if you remember from the macro bolt rates we had this second method here of mix-ins and again all it's doing here is just simply taking each of them and just doing the same exact macro call as we would do one at a time that's it there's nothing fancy about this method it's just a simple for each statement that assigns a macro every time it finds one it's as simple as that so let's create a new directory under app and we'll call it mix ins inside of mix ins let's have a new PHP class which is going to be called xtr mix ins exactly the same as our actual STR class in that way it kind of matches and you have a nice naming convention so how do we declare these mix ins in this cost now at first they look a little bit funky but you kind of get used to it so you're gonna have a public function and then the name of the public function is going to match the name that you want for your actual macro so in this case it'll be partner so we'll put that in so public function part number and then what we have in here is we need to return this entire function so we're going to return this function that we have right here but of course at this point you may be wondering well how does part come into play I am not accepting part inside part number but if you remember this is actually being called a little bit differently than what you think and actually we have an extra parenthesis so let's get rid of it so remember that this function right here is actually only going to be saved into this macros array and then it's going to be called so it's not that we're going to be instantiated and actually calling part number on this specific class all we need is just a function basically just as a self-contained function we're not gonna actually run it until we use that all we're gonna do is just store it inside this array so that's why we can actually have part even though we are not accepting part in the public function itself so okay so why don't we create a second one that doesn't exist yet so let's create another public function and this one's going to be called prefix another made-up function that I will have so we'll return a function out of this one same exact thing and so in here let's go ahead and just return whatever it gets passed in so let's go ahead and accept a string and then we'll accept a prefix and it'll be something like all right prefix dot string so basically I am just concatenate these two things however let's go ahead and default the prefix to something like a B - so again this has to do with part numbers it's just the made-up example that I have for this project so great so at this point we've got these two functions but of course our application doesn't know anything about them so to add them in I will actually comment this out and on my STR instead of calling the macro I can call the mixin in the mixin all you need to do is just new up this new STR mixin class that we created now do notice that STR mixin did get imported up here at the top so that's why it's going to work so at this stage we actually have access to both of these methods so part number and prefix so let me bring back this dye and dump for part number and if we ran everything correctly of course we are still running furthermore we now have this new mixin that we didn't have before which is called prefix so let's go ahead and prefix that and if we hit refresh there we go so we have a B dash because remember we are defaulting the prefix let's change this to a b c d dash and when we hit refresh now we have ABCD so now we have a nice contained place to actually have our macros and I actually think that this is probably the best way to use macros because if you only have these functions declared in the app service provider it's gonna be a little bit difficult for somebody who is not as intimate as you are with your applications source code to find these actual macros however if it's right on the app directory and it's called mix-ins i think it's pretty obvious that we are adding some sort of functionality that doesn't actually come with level so all right so one last thing I want to show you and then we'll be good to go for this lesson so the last thing is sometimes your mix-ins actually have conflicting methods and here's what I want to show you I still have this part number right here and of course let me go ahead and change my part number here to other so what do you think it's going to happen whenever I call part number because remember we are setting a macro of part number and then we're adding a mixin of part number but part number in here is called exactly the same thing so we basically have two macros with the exact same name so who is going to win this battle of course this one is going to get declared first and then the one inside the mixin will get declared so this one right here so let me go back to my example and let me uncomment that part number 1 2 3 and let's see what happens so there we go so right now other is winning so even though we actually had this first our mixin over wrote this up here so as a second optional parameter the mixin method you can pass a boolean so the boolean is false and what we can say with false is do you want to replace anything that is already by the same name and if you hit false by the way the default of this is true as we already experienced we saw that the mixin actually overrode our original part number we already had declared so if you don't want that functionality there we go so passing in false as a second argument in your mixin will allow you to not get your other macros overwritten so a pretty useful function whenever you're trying to compose with several different mix-ins and you just don't want anything to get replaced now the nice thing with this is that if you have a mixin but it has some methods that are only specific to one part of the application you can always override the one macro right before and just make sure you pass in false as a second argument so that's a little bit more of the power user option that you can have with mix-ins but that's gonna be it for this lesson I hope you've enjoyed this lesson on macros and again don't forget to check out this entire blog post and make yourself familiar with all of the classes available that are Mac rabble in there though thanks for watching
Info
Channel: Coder's Tape
Views: 28,624
Rating: undefined out of 5
Keywords: laravel 6, 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 macro, laravel macros, laravel traits, traits php, laravel functions, laravel helper functions, macroable, laravel macroable, macroable laravel, extend laravel, laravel mixins, mixins laravel
Id: S8nz1JqbT9M
Channel Id: undefined
Length: 19min 3sec (1143 seconds)
Published: Tue Sep 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.