3.2 Define the Routes
With most of the work being done in the previous lesson, all we need to do now is define our routes, fix some errors, and test the code. We'll do that in this lesson.
1.Introduction2 lessons, 06:28
2.Create a Sample API2 lessons, 17:23
3.Write the Music Store API3 lessons, 27:11
4.Conclusion1 lesson, 01:04
3.2 Define the Routes
In the previous lesson we set up our database, we created our model, and we also created the guitar's controller so that we can manage our guitars. Now we just need to set up our routes and actually test our application. So we're going to start by changing our default route. We aren't going to return the version. Let's just say, Welcome to the Music Store, and that will be fine, and then we want to define the routes for our to guitar controller. So we're going to group these together because it makes sense to do so. So we're going to call the group method and then we are going to use a prefix of simply guitars. So prefix for the key, the value is going to be guitars and then we also need the name space as well. So, namespace is the key the value in this case is going to be App/Http/Controllers, and then, we need our closure. So, let's do that. We're going to pull in the app there, so that we can set up the remainder of our routes, and we'll start with our index, that's going to retrieve, all of of our guitars. So that's going to be a GET request the URL is just going to be the route of guitars, and let's go ahead and add a comment and we can say HTTP localhost at 8000/guitars. And the method that we want to call is on our GuitarController. So GuitarController at and then that method was index. And that's all that we need to do. So the next is going to retrieve an individual guitar and we need the ID. For our route, and then the method on our controller was find. So let's have another comment here just so that we know what that is, so we will have guitars/. And let's just pick the id of 50. We will eventually have 50 guitars. Any self respecting music store will. So we have our two get requests ,the next is a post request for creating a guitar. So, we will use the POST method. Now, we don't need anything, as far as the route is concerned. The only data that we are sending is the data for the new guitar. So there's nothing in the URL and we're going to use our guitar controller, and that method was simply called create. Now if you'll remember we are using Lumen's dependency injection to give us the request here. And now that I am looking at this with fresh eyes I see what my problem was I had a fat arrow instead of a skinny arrow. And that means I made the same mistake down here and everything else looks okay. Okay, so that error goes away and it turns out Visual Studio codes PHP syntax checking Is actually pretty good. So we don't have to do anything for the requests it's just automatically going to do that. So, let's move on to the put request that's for updating. Now in this case we do have an ID. So our url is going to have a route parameter but once again, we don't have to do anything special for the method designation here. Lumen is going to provide the request to that method. It's also going to pass the ID from the routes parameter. So let's add some comments here just for the URL's. And then the final route is for deleting a guitar and we can copy the put request. Because all we need to do now is change the put method to delete and then do the same thing for the controller and method designation there. So this is our routes. Everything should be set to go. So, in our HTTP debugger, we need to make some requests. The first is going to be a GET request for our guitars, and we shouldn't get anything back. Well we should get a response, but there shouldn't be any data because we don't have any guitars. And that is exactly what we get. But let's create one. So we are going to make a post request. Now we need to add a header here because we need to set the content type. So let's set that to application/json. And then for the request body, let's have the "make";"prs" that's for Paul Reeds Smith and for the model. Let's make this a single cut and of that will be all of the data that we need to send. So if we execute this request and we inspect it, we get back our guitar object with an ID of 1 the make P.R.S. the model single cut and the time stamps. So that is good we are able to create. Let's also go back and make a GET request. Now we can't send any data there so let's execute that request. And what do we get? We get a guitar back. Let's go ahead and make a request for an individual guitar. Ideas one we execute and we should get that guitar back. So now let's update this guitar. Essentially you want to do the same thing that we did with our post request but we need to specify different information. So in this case we're going to make a PUT request and let's say that this is still a PRS. But instead of a single cut, we're going to say that this is a Starla. So we are specifying the ID in the URL. The data that we want to send whenever we executes hopefully we get a 200 and we don't we get a 500. So let's see what went wrong. If you look at the Web view we can actually see the HTML and it says non-static method Illuminate database eloquent model update should not be called statically. Did I call it statically? Well obviously I did as we wouldn't be getting this error so let's see we get the guitar. We call update. Yes, I don't know what I was thinking but it was apparently not what it needed to be so that as an instance method we still pass in the request. But wow, I just got this completely wrong. That needs to be updated and then we say updated. Okay, so that should fix that. We should be able to reissue this request and Fiddler makes that very easy to do. We just right click and replay, we Reissue Requests. Hopefully we get the 200. And we do. We see that that was updated. So let's make a request again for the individual guitar. We will replay that one. And we will inspect and that was updated to starla. So the only other thing that we need to try is the delete method. So we don't need to send any data there, we do need to specify the idea of one. And if we execute this, we get a 500 as well. So let's inspect. We will look at the Web view and it says BadMethodCallException. Call to undefined method Builder::destroy. And it helps If you type it correctly. So we will fix that little typo. It will go back to Fiddler and re-issue that request. So it will go back down to replay and hopefully we get 200. We do, it was deleted and if we make our GET request for all of the guitars, then we should see that we don't have anything and that is the case. So we now have a working API but as it is everything is publicly available. We need to protect the most important parts of our APIs which are the Post put and delete requests and we will do that in the next lesson.