The Laravel Request-Response Cycle
Okay then gang. So in this video, we're gonna go from the ground up and look at how Laravel works on the back end. So imagine that you open up a web browser and you type in some web address and press ENTER to go to that website. Then what happens is it sends a request to the server that is hosting that website.
Now if that server is using Laravel as a back-end like we are going to be doing, then this is basically what happens from a bird's eye perspective. So the request comes in first of all and it's handed off to a route file, and that route file looks at the URL that was requested. So in our case, it's just some site.com forward slash, the base URL. So it looks at that request and decides what to do next.
Now that could either be process some data and save it to the database, or retrieve some data, or do something else. And once we have that, we could inject it into a view, which is just an HTML template. We can compile that and then return it to the user so they can see in the browser. So this at the end of the day is just going to return some HTML to the user, and it might have some data in it that we get from the database.
Okay, so as an example, the forward slash route is going to come in. We'll look at that route, that URL, say, look, this is just forward slash, therefore we probably don't need to get any data. We might do, but we don't do in our case. So what we're going to do is just compile a home view, a home HTML file, and send that to the user so they see the home page in the browser.
If it was forward slash users, then what we might do is look at that in the route file, say, hey, they want to see the users page, therefore we need to get a list of all of the users from the database. We'll get those, then we'll inject them into an HTML view, or a Blade view as you'll see later on, and we're going to compile that into HTML so we can send it back to the user and they get an HTML view which has all the users in it. So this is basically what happens from a bird's eye perspective behind the scene.
Exploring the web.php Routes File
For now, what we're going to do is take a look at this routes file right here and see how it can send a view back to the user dependent on the route that comes in. Okay then, so let's look at this in practice.
First of all, we want to take a look at that routes file because when we first receive some kind of request from a browser, then it's going to go to the routes file first of all. And inside this routes folder, there's several different files. We want to look at this one, the web.php file. So right now you can see we just have one route set up, and we do this using the Route class and then on there a method called get. And what this does is take in two arguments.
The first argument is the route that we expect. So this is just forward slash, that would be like going to, you know, www.site.com forward slash, so the root route, the root page essentially. Right? And what we do when we get that request is fire this function. And this function at the minute is doing nothing but returning a view. So this is how we return a view: the return keyword, then the view method, and then whatever view that we want to return to the browser. That's all this is doing here.
Understanding Views and Blade Templates
But where is this view? Because it's nowhere here. Well, the views are kept in the resources folder. So if we go to resources and go into views, you can see right here we have a view called welcome.blade.php. This right here is a view, and Laravel automatically knows to look inside this views folder for a specific view called welcome. It doesn't matter that blade.php is on the end, it just looks at the name of the file right here: welcome. So it grabs this view and it compiles it into regular HTML, because at the minute this is a .blade.php file. And remember, Blade is a templating engine. Okay, so this is in essence PHP, and this needs to be compiled into HTML before it's sent to the user.
Now Laravel does that automatically behind the scenes. We don't need to worry about that. But all of this is just HTML and a bit of Blade syntax like this. Now don't worry too much about what this means yet. We're going to talk about Blade syntax later on, more specifically authentication towards the end of the series as well. So don't worry too much, but you can see that this is mainly an HTML template with some dynamic content inserted into it. And at the minute all we see is this thing right here, Laravel, and a load of links. So when the request comes in, which is just forward slash, it grabs this view, compiles it into HTML, and sends that to the browser so that we can see.
Okay, so I think down here we still have that browser open. So let me just open this up, and this is the view that we see. So all of the HTML that was inside the editor over here, that represents what we see in the browser over here. Okay, so if we wanted to change this, for example, I could just come up here and say instead something like Pizza House, and then I'll do a <br> tag, and after that we'll say, "the north's best pizzas." Okay, save that and then if I go over here and refresh the browser, then it should update: Pizza House, the north's best pizzas.
Creating a New Route and View
Okay, so what if we wanted to create a different kind of view at a different route? For example, /pizzas. Well, at the minute if we do that then we don't see a page, we get this 404 page instead. And that's because we've not set up a view or a route to handle this request. So let's do that.
Let me go back to the web.php routes file, and I'm going to copy this and I'm gonna paste it down below. So again, we're setting up a new route. And this right here, by the way, this means it's a GET request. And typically a GET request is something we make every time we type something in the browser and press Enter to go and get that web page. That is a GET request.
Now right here we're saying, okay, we're going to set up another GET request handler, and this time I want to handle the /pizzas request. So when they go to mysite.com or in our case localhost/pizzas, then we're going to fire this function. Now at the minute, we're saying return the same view, but what if we want to return a different view, a view called pizzas for example? We can do that, but then we need to create the pizzas view. So let's go over here to the views and say new file and call this pizzas.blade.php.
And by the way, we don't have to use Blade inside our different views. We're gonna be using Blade because it's going to allow us to easily output dynamic data or use logic inside the templates. And we'll see all about that later. But for now, just call it blade.php. Now what I'm going to do is just copy all of this stuff, so Ctrl-A and Ctrl-C to copy that, and I'm going to paste it all inside this file because I'm super lazy. I'm gonna delete these links though, and then I'll just replace this text with pizzas. Okay, so it's gonna look pretty much the same. I'm going to save this file and also the routes file, and I'm going to go to the browser and I'm going to go to /pizzas again, press Enter, and now we see that view because we've set up a route handler in our routes file, and then we've sent back a specific pizzas view when that request comes in up here. Okay, so that's all pretty simple, right? That's the basics of how we handle routes or handle requests that come in when a user tries to see a particular page or URL. We handle that request inside the routes file over here, and then we can return a view.
Returning Other Response Types (Strings & JSON)
Now we don't have to just return views. If we wanted to, we could just return a string. So let me comment that out and just say down here return and then we'll just say pizzas. And if I save this and go back to the browser again and refresh, then we should just see text. And if I inspect and go to the network tab, I'm just going to refresh again. If you click on the pizzas request, then you can see down here that the content type is text/html, okay, and we're sending back just plain text.
Now, if we wanted to, we could send back JSON. So I could say return instead, and then an array, and inside here I could say name is going to be veg pizza, and I'll do a second value. The key is going to be base and the value of that is going to be classic. So this right here is me returning an array, but Laravel is clever enough to look at this array and say, okay, I'm going to turn this into JSON and send that JSON string back to the browser. So let me save this and minimize. I'm going to refresh over here, and we can see now we get this JSON string. And if we click on the request, we can see now that the content type is application/json. So we're not going to be doing this much in this series, I just wanted to show you that we can do that.
For now, what we're going to do is delete those and we're just going to return the pizzas view. So now if I save that and refresh, we should see the pizzas view again. And if I go to just forward slash, then we get the home view or the welcome view instead. So now we know the basics of the routes file and how to return views. In the next video, I want to show you how we can pass data from our route handlers into the views themselves so that we can dynamically output that data and send it back to the user.