Laravel 6 Beginner - e13 - Request Cycle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] up next I want us to start talking about the request object if you understand the cycle of an application you understand that a customer creates a request and then the server comes up with a response up until now most of what we've done has been dealing with the response with the exception of being able to retrieve the customer ID which we did through route model binding like so but I really want to start to focus on the request notice that we used this request helper function let's dive a little bit deeper in understanding what levo is actually doing behind the scene if we take a look at this request here's the actual implementation of this function don't get bogged down with the details of this I just want to show you a little bit of the magic under the hood just so you start to understand a little bit of the concepts behind these functions that you'll see in laravel you may think they are just big magical functions and they're not they are leveraging something called the service container that is an advanced concept in level but all you need to know about the service container for now is that it is a huge array that has every single piece of data that your application needs to run and these could be classes well they could be keys they could be all sorts of things just think of the service container as a huge array of data whenever we do something like app requests we're simply just grabbing the requests inside this huge array that I'm talking about and that's it now this there are different flows here right and one of them would be well if it's no then returned the entire request if you have a key then give me only the particular key so just slightly different implementations the only thing I need you to remember from this is that the service container is a big array that contains classes and other things and that's what these helper functions use whenever I use it this way well you may see in a slightly different way it doesn't matter how you use it because under the hood levo does the exact same thing you may see the request being grabbed in a different way so I do want to show you how that works sometimes you'll see something in here inject it into a particular place why don't we try it that way let's actually do it right here in the index method and let's just go ahead and do it the other way luminate HTTP request then you could also grab your request now if you notice up here it's kind of already imported for us it comes with every single controller that letter of all uses but we don't need to have this full namespace we can simply just have request this request is the exact same request as the other requests and I could prove it to you with a small side-effect we could die and dump that's DD let's go ahead and die and dump this new variable that we just created and inject it over the top I will hit refresh right here and notice that this is a request object it has info to the path it's got the session data in it it's got all sorts of cool stuff in it now I'm going to change this and I will use the helper function instead if I say request as a helper function I'll hit refresh and what do you know I get the exact same thing there is absolutely no difference at all don't people prefer to have it like so and some people prefer to use the helper function I will most of the time use the helper function just because I don't want to have this extra import up here it's just a little bit more overhead that I have to process in my head so I prefer this method but again it is the exact same thing because all it does is it dives into this huge array that is your application called the service container and it gets back the request object now one cool thing that we can do here let me actually revert back since we already have it imported so we use the request and we could say okay give me the query parameters for active okay I hit refresh says there's no let's add a query string so right at the end of this we can add a query string for active and we could say only show me active so notice that now one is what is really reflected anything that I pass in here hello that gets passed through that's pretty cool right so maybe what we could do is we can use a query string to show active and inactive customers using this new request object that we know about so instead of hard-coding a1 why don't I just grab this right here let me copy it and I can erase this entire line and now instead of one I will replace the one with my request active ok so now if I hit refresh I've got 2 but now I could say 0 and now I don't have any customers to show to just like that we are using the request to be able to show inactive and active customers why don't we put this in a link here so we could just make it really easy let's go into that customer that index this one right here let's add some links over the top let's go ahead and do this one here will be slash customers and say active equals 1 and we'll say active customers let's just keep it simple we just say active and then on this one we'll do the exact same thing but this one will be active equals zero okay so these will be inactive customers okay gives us a refresh so now I have active customers and inactive customers and all I am doing is just simply changing this right here would it be nice though to have a default because let's see what happens if there isn't one well it defaults to probably zero is I guess what I could tell here's one cool thing that you could do whenever you're retrieving this as a second argument you can pass through a default but I will say my default will be active customers which will be one now if I hit refresh now even though I don't have one we can tell that it is only grabbing active customers so we can grab active customers this way or just simply not have anything in here and we have a default in place pretty cool right we can say inactive customers active customers and then we have add new customers so let's go ahead and recap everything that we talked about the request is just simply what the user is sending through to the server we have to remember the steps here the request again is what comes in and then the response is what the server sends back we can tap into the request by simply using the request out of the illuminate HTTP request but it doesn't matter how you use it because at the end of the day it always comes from the same exact place and it will return the exact same thing if you recall earlier in the series we use this request function and if we dive into this request function you'll see that all it does is it simply goes into my application this big array that we call the service container and it grabs the request and I prove to you that it does the exact same thing now if we go back up here remember that every controller automatically imports this illuminate HTTP request and we can simply just use it inside one of our methods in this particular case we are using it to figure out if we're going to show active customers or inactive customers now we use this query there are other methods that you can use like input and things like that and they will grab other request objects not only from the query but from a form for example you can use input to grab form inputs but the important concept to understand here is that we are able to tap into the request and use it to our advantage like we did in this particular example
Info
Channel: Coder's Tape
Views: 20,125
Rating: undefined out of 5
Keywords: laravel, laravel 6, php laravel, laravel service container, laravel services, laravel request lifecycle, laravel request, laravel project, laravel 6 tutorial, laravel 6 new features, request laravel, request life cycle in laravel, laravel response, laravel 6 for beginners, laravel 6 crud
Id: alQZvpTNC68
Channel Id: undefined
Length: 8min 9sec (489 seconds)
Published: Sun Nov 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.