From the course: Building RESTful APIs in Laravel

Setting up the RESTful API routes

From the course: Building RESTful APIs in Laravel

Start my 1-month free trial

Setting up the RESTful API routes

- [Instructor] As I mentioned earlier in the course, when creating API route, we work in the api.php file. Within this file, the api URL prefix is automatically applied to the URL. The URL will be different depending on what local development environment you are using. And if you want to, you can even change this prefix from api to anything else in the RouteServiceProvider class. You can find the prefix in the public function boot and you can change it here. In the api.php file, we have two options for how to create the routes. We can list each individual route and map it to the control actions like this. Routes::get, so we are getting data from the petitions URL which is associated with the PetitionController class and specifically the index action. Let's not forget to import a PetitionController at the top of the file. To store a new petition, you would use the Route::post method to the petition's URL which is associated with the PetitionController class, specifically with the store method. And we could do this for all actions in the petition's controller, but however, there is a faster way to do this. Instead of doing each route one by one, we can use the Route::apiResource method, which, again, goes to the petition's URL and is associated with the PetitionController class. The apiResource method will map all of the actions in the PetitionController with all the HTTP verbs in one line of code, which is amazing. There may be a time where the controller only has some actions, such as the index or the show action. In this case, we can use the Route::resource method to instruct Laravel that we only need specific routes. We would say Route::resource, then send it to the petitions endpoint associated with the PetitionController, and then we chain the only method that accepts an array of control actions we want to create. In our case, the index and the store method. In our case, we need all actions. So let's reroute code back and use the apiResource instead. To see that this worked, we can run the php artisan route:list in the terminal to check that all routes have been created. And, as you can see, yes they have. Every action has been assigned a correct HTTP method and an endpoint. Endpoint is where the API sends the request. If you look at these URIs, they all follow the same conventions and they only differ in the method they use and at the end of the endpoint. So this index action, it's URI is /petitions and it uses the GET HTTP method. We can also send the POST method to the same URI, but this is associated with the store action. Then if we want to display a specific petition, we need to specify the petition ID and we use to GET method, and this is all associated with the show action in the controller, and so on. An endpoint is what allows you to customize the request to achieve a specific result or to retrieve specific data. Each feature of an API needs its own endpoints. Let's have a look at some of Spotify's API endpoints. Every API has a base URL to which the various endpoints are added. Spotify's base URL is https://api .spotify.com/v1. If you send a GET request to /albums, you will receive JSON response with all albums. If you send a GET request to /albums/id of an album, you will receive a response with the details of a specific album. And if you send a GET request to /artists/id of a specific artist, /albums, you will receive a response with a specific artist's albums. Now that we have created a model, the controller, the API resource, the migration, the seed, and the API routes, it's time to implement the controller methods.

Contents