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
Since I want all my endpoints to be within the /api/v1/courses route, I can make a route group. Within that group, I can add my routes without having to include the /api/v1/courses every time.
To help organize routes into logical groups, the \Slim\App also provides a group() method. Each group’s route pattern is prepended to the routes or groups contained within it, and any placeholder arguments in the group pattern are ultimately made available to the nested routes.
Since I want all my API end points to
be within the API v1 courses route,
0:00
I can make a route group, app->group.
0:09
And we say ('/api/v1/courses',
0:14
function() use ($app)).
0:22
And we'll put our other
route within this group.
0:32
We have the courses route, but I don't
want to append anything to my route,
0:39
I just want to use the APIv1 courses.
0:44
So instead of courses here,
I'm just going to use an empty string.
0:47
If I put a slash here,
0:51
it would only work with the slash
appended to the end of courses.
0:53
If I want it to work with or
0:58
without a trailing slash,
I could add some middle ware.
1:00
Check the notes for more information.
1:04
Let's check this in a browser again.
1:06
Api/v1/courses.
1:12
Great, my courses are showing.
1:17
Let's add the rest of the get route.
1:21
I'll duplicate this route
to build my next get route.
1:25
We're going to add the course_id.
1:32
This is going to get a single course.
1:36
So we get a single course and
pass the ($args['course_id']).
1:39
We also want to be able
to get our reviews.
1:47
These reviews will all
reference a single course, so
1:50
we can create a second group
within our first group.
1:55
$app->group, and
2:02
we'll add the course_id and reviews.
2:06
We'll use the app, And
then end our semicolon.
2:19
Now we can copy our route
to get a single course.
2:25
We'll paste this within our reviews group,
and we'll change it,
2:31
To getReviews, ByCourseId.
2:39
We don't need this course_id since
the course_id is within our group.
2:48
Instead of course,
we need to change this to review.
2:53
Now for our final get route,
we can copy this route,
3:00
And we'll use it to
select our single review.
3:08
So we'll add review_id.
3:12
$this->review->getReview, and
3:17
we'll pass the review_id.
3:21
Let's take a look at these
requests in the browser.
3:25
We have our courses.
3:31
We have our single course.
3:35
And our reviews have not been defined.
3:42
We have our review model, but
let's add it to our dependencies.
3:46
We'll need a new container for review.
3:56
And we'll close our semicolon.
4:08
We'll return our new App\Model\Review.
4:09
And again, pass the get('db')).
4:16
Now we can see our reviews,
And a single review.
4:25
A browser isn't really designed
to test API calls, so for
4:32
our other HTTP requests, we're going
to jump into a program called Postman,
4:36
which gives us easy access
to the request and response.
4:42
You need to sign up for Treehouse in order to download course files.
Sign up