Browsing records is the basic technique in an API to get a collection of records. Learn about and build the code necessary to get a collection of polls from your API.
- [Instructor] Our first goal with our API is to build the ability to get the records for an endpoint. In this case, we want to get all of the polls in our database. In this case, it's just going to be a simple API endpoint to get all the polls. To begin with, we need a controller, so we'll make a new controller to manage polls with. In this case, using Laravel's make command. To run this, open up your terminal application into the community-poll directory and run the command php artisan make:controller PollsController.
After this, we'll need to build a route for us to be able to hit the PollsController. We'll open up our code editor and in here, we'll open up the routes directory and open up the api.php file. We'll add on line 19 Route::get, pass the string polls, and then the string PollsController@index. This says when we send a GET HTTP request to the polls' route, route the action to the PollsController.
And then the index meant that they're in. Since this is in the API call rather than the web file, our routes will all have a prefix of API applied to them. Now, let's see what happens if we start our application. Go back to your terminal application and run the command php artisan serve. In our browser, we'll go to 127.0.0.1:8000/api/polls.
At this point, we get an error page and that's because we don't have an index method in our PollsController class. Now, we're going to go create one. We'll open up the PollsController file in our text editor, which is at app, Http, Controllers, PollsController.php. On line nine, we'll replace the comment with a new empty public function index. If we save this and refresh the page in our browser, we no longer at least having the error page displayed, but of course we don't see anything as the controller method isn't actually doing anything.
So to finish up, we need to load in the polls and display them. Go back to our file, and in the index function, add the line return response, which is going to be a function call, arrow, json will call Poll::get(), 200, with a semicolon at the end. This says to return a response that is a JSON formatted response with the HTTP response code 200, and it's going to get the polls from our database.
At this point, we also need to add before line five, use App\Poll, so we include the Poll object into our class. Now we can go back to our browser and refresh, and there you'll see all of our polls formatted in JSON with the 200 response code. And that's how easy it was for Laravel and for us to build in a basic endpoint.
- Reviewing the basics of an API
- Basic pieces of the Laravel framework
- Reading, adding, editing, and deleting a record
- API errors and exceptions
- Transforming your API data
- Returning nested and sideloaded data
- Creating a logging middleware
- Authenticating to your API
- Basic and advanced endpoint testing