Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Lumen 3
  • Overview
  • Transcript

2.1 Define Routes

Like Laravel, HTTP requests are routed to PHP code that executes and returns a response to the client. In this lesson, you'll learn how to route HTTP requests to Closures.

2.1 Define Routes

In the previous lesson that we installed Lumen, we created a project and then we ran it and if you'll remember this is the response that we got. It basically just gives us the version of Lumen. Now whenever we ran our PHP server, we did so by specifying the public folder, so let's start there. If we go to the public folder, there is an index.php file. So whenever we open that, we see that there are two lines of code. The first is pulling in a PHP file called app.php inside of the bootstrap folder and then it simply calls the run method. So let's go there bootstrap, app and here we see another simple file. Most of the code that's in here is commented out. But there is these lines here. Now we can see that we are using this group method, where we are specifying a namespace for our controllers and then there is this closure that is requiring our routes. So let's look there. The routes.php file inside of app, http and then routes is where you define your routes. And we can see here why we get the version of Lumen. For every get request for the route of our application, it is returning the version of our app. So if we change this to just say Hello Lumen and if we go back to the browser and refresh, we are going to see that change reflected in the browser. So as far as our routes are concerned, the basic syntax is this. App and then the method name of the HTTP request. So for handling a get request, that is the get Method. If we want to handle a post request, we do so with the post method. And then the URL that we want this route to handle and then in this case we can write a closure. Now we don't need the app in this case, we're just going to respond with some text. So we're going to say Hello, Post request handled by Lumen. So let's save this. Let's go to our HTTP debugger and let's make that request. Now if you're using Fiddler, what you want to do is go to the Composer tab on the right side of the screen. So Composer and then for the URL, we can see here localhost at 8000. You want to specify a post request and then if you wanted to include any headers, you could do that. Then just click on Execute and it's going to send that HTTP request to our application. We can see that the result is 200, the protocol was HTTP and the host and the URL. So double-click on this and we see the results. In the top part we have the request information and then in the bottom we have the response. And the text of our response is Hello, Post request handled by Lumen. Now every web API framework needs to give us the ability to capture information from the URL, because that is after all how we get input to work with. Like for example, let's say that we have a list of users that we want to work with through our API. So the URL would be /users and then if we wanted to get the data for a specific user. We could specify the ID or the username or something along those lines. Well, with Lumen, we have what are called route parameters. They are surrounded by a pair of curly braces and then inside of the curly brace, we just have whatever we want that parameter name to be. So in this case, we could say username. And then in our closures parameter list, we could say username and then any request that matches our URL is going to use this route and the second segment in that URL is going to be the username. So instead of saying Hello, Lumen, we could say Hello and then username. So let's go back to the browser and we need to change our URL here. We now have /users and then a username. I'm just going to use the one that I always use jwmcpeak and we see that in the browser as well. And naturally, we can have multiple parameters within a single URL. So let's change this again and let's say that we are writing an API for a blog. So we'll say posts and then we will have the postId, let's also get a specific comment for this post. So we'll say comments and then we will have the commentId. And then for our closures parameters, we'll say postId and then commentId. So the names don't have to match between our URL parameters and our closure parameters. But they are positional based, so because post is first, our post ideas first for our closure. The comment is second, so our commentId is second. So inside of our closure let's just say Hello and then we will pull in the postId, then lets also concatenates comment. And then the commentId. So let's go back to the browser and we will change our URL once again. So we have posts and then our postId which is just choose the number 1, comments and then for our commentId, we'll use the number 10. So we will see Hello 1 comment 10. The last thing we'll look at in this lesson is the group method which is used to group multiple routes together that share similar attributes. And we've seen the group method being used inside of app.php, there it is right there. It's grouping using a namespace and in fact it's assigning this namespace and we can see in the comments what it does. This will provide all of the URLs, the application can respond to, as well as all of the controllers that may handle them. Now we're not going to deal with controllers just yet, that's something that we will look at in the next lesson. But for now let's use the group method to group routes based upon a prefix. And what do I mean by that? Well, let's look at this route that we have here. We are retrieving a specific comment for a specific post. But we would have other routes as well. Like for example, we would say that we wanted to retrieve all of our posts which would just have posts for the URL. And then our closure, which would return all of the posts. So we could say all posts there and then we would have another route to retrieve a specific post. So the routes would be /posts and then the postId and the closure would have the postId and we would just return the post with Id and then postId. So you get the idea, we would have a multiple routes specified for all of the posts URLs. But with the group method, we could group all of these together based upon the prefix of posts. So we would do that with app and then group. And then for the first argument we would pass in an array. The key would have be prefix. And the value would be our prefix posts in this case and then we would have our closure but we need access to our app. So we will say use app and then we will basically take all of these other routes, cut them out and then paste them inside of our groups closure here. And since we have specified posts as a prefix, we don't have to use that in our URLs. So we can just say our post for our specific post and then for the specific comment for a specific post, we'll have post and comments and then commentId. So we're going to end up with the same results at least as far as this route is concerned. But we now have two other routes that we can play with. So let's go back to the browser. Let's refresh the page and we have the same results. But we also want the posts with an Id of 3. So we see post with Id of 3, we see that that was being handled by that new route that we had it in. But if we wanted to retrieve all of our posts, then we can just say /posts and then all posts. So now we know how to handle HTTP requests by defining routes. And we did so with very simple closures which would work in a variety of circumstances. But there are other times when we would want to use a controller to handle a request and we will look at that in the next lesson.

Back to the top