Laravel 5.8 Tutorial From Scratch - e28 - Events & Listeners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back we are back in our original project that we've been working on throughout the lessons what I want to show you today is a brand new concept and maybe a brand new concept to you all together and it is the concept of events events are an invaluable tool in laravel and you're gonna end up reaching for them quite often think of an event as exactly what it is something happened when something happens an event has occurred and at that point you'll have a list of things for your application to respond with the classic example of this would be a brand new user registering to your app when they do that you more than likely are going to send them a welcome email and then you may need to subscribe them to your newsletter and on top of that you may want to get a slack notification saying hey there is a new subscriber to your app so all of this can be done at the controller level and we're gonna start off by doing that and then we're gonna refactor it to an event and that's really the place where it belongs so before we get started let's head back to the terminal and I'm gonna get our development server booted up again so PHP artisan serf and there we are let's visit the page all right so we are here so here's what I'm thinking we have a full customer list controller working already so my first idea would be if we added a new customer I want to send them a welcome email pretty simple but pretty common thing you need to do in a lot of applications let's head back to phpstorm and let's pull up the customer controller there we go and of course we know that that's gonna happen whenever we call the store method and this is the beauty of using restful controllers everything always falls in the same place we show them the form during the create and then we hit that store method to actually be able to store our customer all right let's start a new line here now we already talked about mailable and I'm just gonna do it very quickly it is more of a review if you're not familiar with mailable x' there's a whole lesson on how to actually send email in lerigot so check that out before you move on so let's go ahead and make a new mailable now I'm gonna switch over to a different tab now I'll say PHP artisan make male welcome new user male and we'll say that this is gonna be a markdown and we'll put that on their emails dot new welcome for now all right head back over to phpstorm let's check out new welcome I'm gonna keep this extremely simple I'm just gonna have welcome new user nothing fancy nothing crazy alright so we know that in our controller we are creating a brand new customer and then we're gonna send them that welcome email now the create method will actually return us the new model so I'm gonna save that to a variable because I'm gonna need their email and let's go ahead and do mail and that's illuminate support façades mail notice I am importing it here at the top right here you gotta add that otherwise you're gonna get a mail not found exception and that's gonna be - and we're gonna send it to customer email and we're gonna send a new welcome new user mail ok then we set something like we want our user to be registered to our newsletter we're gonna fake that one so I'm just gonna do some pseudo code here registered to newsletter and there's obviously some real logic that you would put here I'm just gonna dump registered to newsletter and then the last example that I gave you was that we wanted to send a slack notification to the admin and I'll just say dump slack message here so we have three distinct steps right we are sending an email then we'll go ahead and register them to the newsletter and then we're gonna send a slack notification and then finally we are going to redirect now if I run this right now we're gonna end up going back to customers and we're not gonna get to see these two steps right here just for demonstration purposes I am gonna just disable that return so that way we actually get to see those events have so I will just comment out that for right now again I'm only doing this for demonstration purposes otherwise we would never get to see these messages alright so let's see what that looks like in the browser let's add a new customer new user test a test add customer and sure enough we get registered to newsletter slack message here and let me go ahead and pull open mail trap that should still be set up for us so in mail trap we have welcome new user awesome so all three events occurred when we registered a user and this is typical of a new user getting registered to your site there's definitely gonna be a number of things that you need to do but you definitely don't want your user to have to wait to do all these things and of course we're gonna end up talking about queues at some point but this is more about organizing your code properly so let's take a look at the event option to see how we can refactor this right now so when a new customer is registered what I really like to do is just have a single line here of event new and then we'll say new customer has registered and that's it and then the rest of these lines I want those out of my controller like I always mentioned before I want the controllers to be as clean as possible you don't want your controllers to have a bunch of methods and a bunch of lines in them the controller is the 30,000 foot overview of your application not the nitty gritty stuff the nitty gritty stuff does not belong in your controller so now we raise this event which is basically announcing that is new customer has registered and then if anybody cares you need to do what you need to do based on the fact that that event happen and that's the way it works now that side effects stuff those are called listeners and that makes sense they're out there listening for a specific event to occur and when it happens they respond in a certain way fair enough so how do we create these so the first way I'm going to show you is the manual way where you create your listeners and your events separately and then I'll show you a very cool trick to do it all in one command it's awesome so let's head back over to the terminal and run PHP artisan as always let's check out the command first so make event and we have make listener two different things that we need to do remember the event is simply what actually gets called hey there's a new customer that's it that's the only thing the event needs to do then you're gonna make a listener for each of the subsequent actions so in our case say back to phpstorm we have step 1 step 2 and step 3 if you wanted to think of them as steps so we have one event and three listeners that we need to make let's make one event PHP artisan make event and I decided to call it new customer has registered and I will actually add event to the end of that so we'll say new customer has registered events all right I'm gonna copy that I'm gonna head on over and I'm gonna run the command PHP artisan make event and then the name of the event is gonna be new customer has registered event all right let's get that created head back to PHP storm and let's dive through the directories so you could see what happens now here in the app now we have this event directory and inside events we have the new customer has registered event okay inside this event you can accept any number of things for example most of the time to the event you're gonna pass any relevant data so if you wanted to pass in the customer let's go ahead and copy the customer and you could pass that in right here right into the event and then in our event we can accept that customer customer and then we can actually initiate those fields and get them set properly phpstorm did it for me but we added this line right here private customer and then we're accepting the customer here and we're assigning that variable customer to this customer so one nifty trick about events is that this customer variable here if you make this public you actually can access it right in your listener and that's very powerful so I'm going to change this over to public customer and that way we're gonna have access to it and I'll show you how we're actually gonna use it once we get our listener let me go ahead and erase this just clean up this class a little bit all right and there we are there is our event all right so when a new customer registers we're gonna fire an event and say hey new customer has registered and here's the customer so like I said before you make your event and then we need to make a listener for each of the steps let's make a listener now PHP artisan make listener and what is the first step well the first step is we need to welcome our new customer all right so welcome new customer so the full class name will be welcome new customer listener all right let's make that now head on back and now we have a new directory all together here listeners so let's welcome our new customer and really all I have to do is grab this right here I'm gonna cut it out and now we'll bring it into my listener right in the handle method now mail class does need to be imported so I'll import now and that's being imported here at the top right here and let's clean everything up a little bit we don't need a constructor we need to import the Welcome new user email as well and that's what the class looks like now customer how do we access customer well the handle method receives the event so we can say event arrow customer arrow email okay so when we get the event we can directly access that property right from the event so that's how we have access to the email so we can send to our user now we need to hook all of this up right so right now we have individual events and individual listeners and nothing is really talking to each other yet you need to create that connection so that connection is made inside an events service provider we haven't touched on service provider this is actually the first time we've ever touched in service providers but just know that a service provider at the very core of it all it is is just a way for us to connect functionality in our app that's the very simple of it we're gonna dive deep through that later on so don't worry too much about it to find the service provider that's going to be inside the providers folder and there's this event service provider right here in events service provider there is this array right here and this array is a listeners mapping for our application basically what we need to do is we need to connect our event with each of the listeners remember we're gonna have three listeners eventually but we only have one event so let's add our event now so our event is new customer has registered event okay and then we have one listener so we'll put that here and our listener is welcome new user listener or welcome new customer listen okay so now we know when a new customer has registered event occurs we need to also run the Welcome new customer listener and that's it now they are connected all right let's check our work and all I really need to do is just refresh this page here now notice here I only have one email if we did everything correct and of course I'll have a second email so I'm gonna hit refresh it's gonna say do you want to do this action again I'll say yes go ahead and it looks like we forgot to import a class somewhere new customer has shared event in the controller alright let's go back to customer controller and it's not working because I created this before I actually created the event so all we need to do is import that class and that gets imported right here right at the top if you ever see a notification like this just check if you imported the class now a lot of times in the video it's not really clear when that happens because phpstorm kind of does it automatically for me but again if you ever don't find a class just make sure that you are importing the class at the top with the proper namespace all right let's try that one more time I'm gonna hit refresh okay so this time it works and let's check out our new email so we see here that we have our new email so that's still being sent remember that I told you I was going to show you two ways a manual way and a more automatic way of actually registering listeners and events and here's the more automatic way so you could come to the event service provider and just give it the classes that you want to make so I'll show you how that works so we have our first welcome email and in the next step for a customer is to register them to newsletter and the third step is that slack message all right so let's say register customer to newsletter and notify admin via slack so those are two brand new listeners that we don't have yet so this automatic way will actually generate them for us however we do have to give it the full name space in order for it to work properly so I'll do that now I will add that in and just to keep it uniform I'm gonna add it to the first one as well and then just stop importing it here at the top so that way it's all clean and ready so there you are this is our event service provider now let's go back to the terminal and now take a look at PHP artisan so PHP artisan has another command here called event generate an event generate will actually look at that Evette service provider and generate any of these classes that it doesn't fund all right let's run that now PHP artisan event generate alright let's head back and sure enough now we have three listeners not just the one now just as a quick note we did create these first two manually but you can make it from scratch you can start from the very beginning and generate exactly what you want including the event and any listeners right away from the very beginning so let's go ahead and distribute our code now notify admin via slack let's clean this up a little bit and I'm just gonna bring this dump over all right let me delete that paste all right so our notify admin via slack all it's gonna do for now is just dump some code all right let me close that up now register customer to newsletter alright let's head back to my controller and let's register our user and we can clean this up by deleting all of that head back again we can get rid of the constructor for now we don't need that paste that in hit safe and let's take a look at our controller now I'm gonna uncomment that line just so we see the full controller so we have three lines when we originally started our lesson we had two lines and now we have three lines and that is very very clean this is the proper way of doing this if tomorrow for some odd reason we decided that one of those steps is no longer necessary or you need to change a step you only go and change that particular listener and that's the beauty of it all right so let's run this code and let's make sure that everything is still running for us I will go ahead and comment that line out one more time just so that we see what's actually going on and let's head back here I'm gonna hit refresh and of course we still see our two lines and we have our email so everything is working exactly the same way except now we're raising an event and listeners are taking care of each of those steps for us now let's head back to event now I want to show you just a little bit of the power of this let's say that tomorrow you no longer want to register a customer to the newsletter all you need to do is delete that line right there we hit save let's head back hit refresh and now the steps have changed now we are sending an email there's our email and our slack message got sent we are no longer subscribing them to newsletters and that is a very powerful way for you to have all of your code contained in one specific place in this case inside a listener for you to be able to execute that code when you need it and if tomorrow you no longer need it all you need to do is remove that line you can delete your classes if you don't need them anymore and you're good to go the other thing is reusability if you have a similar event where maybe you can manually register a new customer for example all you have to do is add an event and go ahead and use the same listener there's no rules against using the same listeners for two or three or as many events as you have so if welcoming a new customer is something that you do in several places in your application you only need the one listener as long as obviously the same email is being sent so that is events now we're starting to get a little bit deeper into level and things are getting a little tough but hang in there continue to watch the lessons over and over follow with the project and you'll see over time how all of this stuff starts to really make sense on a very very last note right now we are running this class and all these events are just running right away but we're going to talk about queuing and what we're actually going to do is we're going to defer these listeners for later on we're gonna let our application take care of those off to the side not blocking the user because of right now for example in my notify admin via slack if I just said sleep hit safe come back hit refresh continue nothing's happening right everything is running in line and something like resizing an image for example such as a profile say that there was a profile image that you needed to resize it may take a little bit of time to upload and then to resize and all of that and basically the user is just gonna be waiting the same exact time that we are waiting right now I'm gonna actually X this out because that's gonna go on for a really long time so that's a bit of a cliffhanger right we want to be able to handle these events off to the side and once we start talking about queues that's how that works but for right now I want to show you one last little thing and that's inside the dot EMB file let's take a look at our queue connection right now is set to sink and sink basically means do everything in sync as soon as they hit the button everything's going to run all one after the other but that's not typically how you run so just keep that in mind this is another one of those very advanced concepts but we're going to handle those and I'm going to show you how to deploy that using four and how easy it is to actually have queues in your application so with all of that if you have any questions as always leave them down in the comments below don't forget to subscribe share the videos help spread the channel thanks for watching and I'll catch you in the next lesson
Info
Channel: Coder's Tape
Views: 60,216
Rating: undefined out of 5
Keywords: laravel 5.8, laravel 5.8 new feature, laravel tutorial, laravel news, laravel new features, laravel best practices, laravel 5.8 tutorial, laravel 5.8 what's new, laravel 5.8 install, php framework, php framework 2019, php what's new 2019, laravel, laravel 5, laravel from the ground up, laravel from scratch, laravel for beginners, laravel events tutorial, laravel listeners, laravel listener event, laravel 5.8 events, laravel 5.8 listeners, laravel 5.8 event listeners
Id: 6wZKwJQF7Is
Channel Id: undefined
Length: 20min 19sec (1219 seconds)
Published: Fri Mar 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.