WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.126 --> 00:00:06.836 align:middle
Now that we have a new page, at your
terminal, run debug:router again.

00:00:07.606 --> 00:00:10.046 align:middle
Yep, there's our new endpoint!

00:00:10.886 --> 00:00:15.526 align:middle
Notice that the table has a column
called "Method" that says "any".

00:00:16.206 --> 00:00:22.146 align:middle
This means that you can make a request
to this URL using any HTTP method -

00:00:22.226 --> 00:00:26.046 align:middle
like GET or POST - and it will match that route.

00:00:26.566 --> 00:00:30.786 align:middle
But the purpose of our new
API endpoint is to allow users

00:00:30.786 --> 00:00:34.326 align:middle
to make a GET request to fetch song data.

00:00:35.016 --> 00:00:40.016 align:middle
Technically, right now, you could also
make a POST for request to this...

00:00:40.016 --> 00:00:41.596 align:middle
and it would work just fine.

00:00:42.146 --> 00:00:49.266 align:middle
We might not care, but often with APIs, you'll
want to restrict an endpoint to only work

00:00:49.266 --> 00:00:52.906 align:middle
with one specific method like GET, POST or PUT.

00:00:53.836 --> 00:00:57.786 align:middle
Can we make this route somehow
only match GET requests?

00:00:58.256 --> 00:01:01.496 align:middle
Yep! By adding another option to the Route.

00:01:01.496 --> 00:01:06.546 align:middle
In this case, it's called
methods, it even auto-completes!

00:01:06.976 --> 00:01:09.836 align:middle
Set this to an array and, put GET.

00:01:09.906 --> 00:01:13.896 align:middle
I'm going to hold Command and
click into the Route class again...

00:01:14.306 --> 00:01:15.866 align:middle
so we can see that...

00:01:16.216 --> 00:01:18.976 align:middle
yup! methods is one of the arguments.

00:01:20.776 --> 00:01:24.636 align:middle
Back over on debug:router: Nice.

00:01:25.276 --> 00:01:27.926 align:middle
The route will now only match GET requests.

00:01:28.646 --> 00:01:31.196 align:middle
It's... kind of hard to test this,

00:01:31.376 --> 00:01:36.226 align:middle
since a browser always makes GET
requests if you go directly to a URL...

00:01:36.796 --> 00:01:41.626 align:middle
but this is where another bin/console
command comes in handy: router:match.

00:01:42.256 --> 00:01:48.416 align:middle
If we run this with no arguments: It
gives us an error but shows how it's used!

00:01:49.576 --> 00:01:56.156 align:middle
Try: php bin/console router:match
/api/songs/11 And...

00:01:56.486 --> 00:01:58.246 align:middle
that matches our new route!

00:01:59.356 --> 00:02:05.556 align:middle
But now ask what would happen if we
made a POST request to that URL with --

00:02:05.896 --> 00:02:12.596 align:middle
method=post: No routes match
this path with that method!

00:02:12.996 --> 00:02:17.376 align:middle
But it does say that it almost
matched our route.

00:02:17.506 --> 00:02:20.716 align:middle
Let's do one more thing to
tighten up our new endpoint.

00:02:20.716 --> 00:02:24.976 align:middle
I'm going to add an int type-hint
to the $id argument.

00:02:26.316 --> 00:02:33.436 align:middle
That... doesn't change anything, except that
PHP will now take the string id from the URL

00:02:33.546 --> 00:02:39.826 align:middle
that Symfony passes into this method
and cast it into an int, which is...

00:02:39.976 --> 00:02:44.336 align:middle
just nice because then we're dealing
with a true integer in our code.

00:02:45.276 --> 00:02:47.966 align:middle
You can see the subtle difference
in the response.

00:02:48.816 --> 00:02:51.536 align:middle
Right now, the id field is a string.

00:02:52.516 --> 00:02:57.326 align:middle
When we refresh, id is now
a true number in JSON.

00:02:58.046 --> 00:03:00.906 align:middle
But... if somebody was being tricky...

00:03:01.036 --> 00:03:05.116 align:middle
and went to /api/songs/apple...

00:03:06.166 --> 00:03:13.626 align:middle
yikes! A PHP error, which, on
production, would be a 500 error page!

00:03:14.046 --> 00:03:15.356 align:middle
I do not like that.

00:03:16.316 --> 00:03:18.016 align:middle
But... what can we do?

00:03:18.556 --> 00:03:24.666 align:middle
The error comes comes from when Symfony tries to
call our controller and passes in that argument.

00:03:25.346 --> 00:03:29.736 align:middle
So it's not like we can put code
down in the controller to check

00:03:29.736 --> 00:03:32.966 align:middle
if $id is a number: it's too late!

00:03:33.276 --> 00:03:37.896 align:middle
So what if, instead, we could tell Symfony

00:03:37.896 --> 00:03:44.586 align:middle
that this route should only match
if the id wildcard is a number.

00:03:45.396 --> 00:03:46.516 align:middle
Is that possible?

00:03:46.956 --> 00:03:52.536 align:middle
Totally! By default, when you have
a wildcard, it matches anything.

00:03:53.146 --> 00:03:57.276 align:middle
But you can change it match
a custom regular expression.

00:03:58.136 --> 00:04:10.166 align:middle
Inside of the curly braces, right after the
name, add a &lt; then &gt; and, in between, \d+.

00:04:10.166 --> 00:04:14.756 align:middle
That's a regular expression meaning
"a digit of anything length".

00:04:15.626 --> 00:04:17.826 align:middle
Try it! Refresh and...

00:04:18.286 --> 00:04:20.616 align:middle
yes! A 404.

00:04:21.176 --> 00:04:25.146 align:middle
No route found: it simply
didn't match this route.

00:04:25.146 --> 00:04:27.136 align:middle
A 404 is great...

00:04:27.536 --> 00:04:29.156 align:middle
but a 500 error...

00:04:29.306 --> 00:04:31.176 align:middle
that's something we want to avoid.

00:04:31.976 --> 00:04:35.906 align:middle
And if we head back to /api/songs/5...

00:04:36.146 --> 00:04:37.836 align:middle
that still works.

00:04:38.736 --> 00:04:44.336 align:middle
Next: if you asked me what the most
central and important part of Symfony is,

00:04:44.746 --> 00:04:47.926 align:middle
I wouldn't hesitate: it's services.

00:04:48.816 --> 00:04:54.966 align:middle
Let's find out what a service is and how it's
the key to unlocking Symfony's potential.

