This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Welcome back! In this section, we'll cover how to build a few basic routes so we can test our Authors and Books resources. See you there!
Eloquent
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Nice job.
0:00
We created a route for our authors in
books which successfully returned a list
0:01
of authors from the author controller and
books from the book controller.
0:06
Now we're ready to create our resources
for both our authors and books.
0:12
Before we get started, let's take a look
at the authors returned by our API.
0:17
And you'll notice that in
addition to the name, title,
0:23
company, and email,
we also have an id, created at and
0:28
updated at, which we don't want
to show to our API consumers.
0:32
Hiding database specific data is
a best practice when building an API,
0:38
but you can also use resources
to hide passwords or
0:43
other sensitive data which
aren't database specific, but
0:47
is an obvious advantage when
building a secure REST API.
0:51
In the next video,
we'll create resources for
0:55
our authors and books,
while using the author and
0:59
book models as guides to see exactly
what data we want our API to display.
1:02
Let's dive right in and
create our first resource like this.
1:09
By default, the resource method
we just created will run
1:24
a two array function, which gets and
returns all attributes.
1:29
To illustrate how this works,
1:34
let's import the AuthorResource in
the AuthorController like this.
1:36
Now we can wrap the AuthorResource
as a collection around the Author
1:43
model like this.
1:48
Moment of truth, let's test
the authors like we did before using
2:06
the welcome view or by copying and
pasting the URL, like this.
2:10
Notice how nothing changed.
2:18
This is because we're simply
passing in all resources,
2:20
which we can change by defining only
the resources we want to expose.
2:24
To see this in action, let's only expose
our author names using an array like this.
2:29
Now, all we see are just author names.
2:45
How cool is that?
2:48
Let's keep going and
finish the author resource by exposing
2:50
the names as well as the title,
company and email like this.
2:54
Let's test the authors
in the browser again.
3:03
And notice how the author resource
hides the database specific data.
3:06
This is because we're only
passing in the name, title,
3:11
company and email without the ID or
timestamps.
3:16
Nice job.
3:20
Great, we're now exposing only the data
we actually want to return to our
3:23
API consumers,
without any of the database specific data.
3:28
Mission accomplished.
3:33
Now that our author controller at index
function is done, let's start working
3:35
on the author controller at show
function in the next section.
3:40
See you there.
3:44
Before we can start working on
the author controller at show function,
3:46
we need to create the endpoint.
3:51
So let's head over to the routes file
api.php and add another route like this.
3:53
Next, let's open
the AuthorController.php file.
4:02
And notice that we are simply
grabbing the author dollar author and
4:06
returning that author using response
dollar author with a status code of 200.
4:10
Now that we have our new route defined,
4:18
let's view a list of all routes currently
available in our API like this.
4:21
Nice, as you can see,
we have our routes defined.
4:29
So let's keep testing our routes.
4:33
You can use Postman,
which we will do later in the course.
4:36
But for now,
make sure your API is running and
4:39
navigate to the author's
route in the browser,
4:44
then simply add a one
after the URL like this.
4:48
Feel free to try two and three.
4:52
But that's all of the authors we have so
far.
4:55
Using four and above will return a status
code of 404 Not Found, which is expected.
4:58
Great job.
5:08
We're now showing one specific author and
everything looks great,
5:10
except that our one author is
once again showing everything,
5:14
such as the ID and timestamp data.
5:19
Instead of returning everything
in the to array function,
5:22
like we demonstrated earlier, we can
define each attribute of our author and
5:27
book models within an array of
the AuthorResource.php file.
5:33
First, we need to open the author
controller file and return a new
5:38
author resource by wrapping the author
inside of an HTTP status like this.
5:43
Nice, remember,
if you're working with one resource,
5:55
use new author resource but
when working with an array of authors,
5:59
you need to return a collection
like we did in the index function,
6:05
which passes in the author
all to get all authors.
6:10
Remember, it's best practice to
remove database specific information,
6:14
such as the ID for example so
everything is looking great.
6:20
Let's view our work in the browser.
6:24
Look at that, no ID or timestamp data,
everything is looking great.
6:29
Now that we're only exposing
the attribute you want to show for
6:35
the @show function, we can start building the @store function.
6:40
But first, let's create the route
like we did with the app index and
6:44
add show functions like this.
6:49
Now that we have our new route defined,
let's view a list of routes like this.
6:54
Great, it's there, as expected.
7:03
Now, let's navigate into
the author controller and
7:06
take a look at the Add store function
in the fields we want to store.
7:10
Great, we now know that we want to expose
the name, title, company and email.
7:18
Next, we want to wrap the newly created
author with the author resource like this.
7:25
Great job.
7:42
In the next section,
7:43
we'll finally start testing our API
endpoints using Postman and REST client.
7:44
See you there.
7:50
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up