WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:00.996 --> 00:00:03.526 align:middle
I see Symfony as two big parts.

00:00:03.976 --> 00:00:08.076 align:middle
The first half is the route,
controller, response system.

00:00:08.566 --> 00:00:10.816 align:middle
It's dead simple and well...

00:00:10.906 --> 00:00:12.986 align:middle
you're already an expert on it!

00:00:13.546 --> 00:00:19.936 align:middle
The second half of Symfony is all about the
many useful objects that are floating around...

00:00:20.176 --> 00:00:22.096 align:middle
just waiting for us to use them!

00:00:22.606 --> 00:00:28.466 align:middle
For example, when we render a template, what
we are actually doing is taking advantage

00:00:28.466 --> 00:00:32.336 align:middle
of a Twig object and asking
it to render a template.

00:00:33.096 --> 00:00:38.206 align:middle
There's also a logger object, a cache
object, a database connection object,

00:00:38.476 --> 00:00:43.056 align:middle
an object that helps make API
requests, and many, many more!

00:00:43.576 --> 00:00:49.016 align:middle
And when you install a new bundle,
that give you even more useful objects.

00:00:49.676 --> 00:00:53.506 align:middle
The truth is that everything
that Symfony does is...

00:00:53.506 --> 00:00:56.516 align:middle
actually done by one of these useful objects.

00:00:56.706 --> 00:01:00.496 align:middle
Heck there's even a router
object that's responsible

00:01:00.496 --> 00:01:03.356 align:middle
for finding the matching
route for the given page!

00:01:04.316 --> 00:01:09.936 align:middle
In the Symfony world, and really the object
oriented programming world in general,

00:01:10.176 --> 00:01:15.506 align:middle
these "objects that do work"
have a special name: services.

00:01:16.166 --> 00:01:17.766 align:middle
But don't let that word confuse you.

00:01:18.186 --> 00:01:23.016 align:middle
When you hear service, just think:
that's an object that does work!

00:01:23.496 --> 00:01:26.316 align:middle
Like a templating object that renders a template

00:01:26.576 --> 00:01:29.906 align:middle
or a database connection
object that makes queries.

00:01:30.506 --> 00:01:34.056 align:middle
And since service objects do
work, they're basically...

00:01:34.206 --> 00:01:36.936 align:middle
tools that help you get your job done!

00:01:37.756 --> 00:01:41.356 align:middle
The second half of Symfony
is all about discovering

00:01:41.356 --> 00:01:44.456 align:middle
which services are available
and how to use them.

00:01:45.346 --> 00:01:46.466 align:middle
Let's try something.

00:01:47.146 --> 00:01:50.496 align:middle
In our controller, I want to log a message...

00:01:50.646 --> 00:01:53.076 align:middle
maybe some debugging message.

00:01:53.996 --> 00:01:58.026 align:middle
Since logging a message is
work, it's done by a service.

00:01:58.406 --> 00:02:01.616 align:middle
Does our app already have a logger service?

00:02:02.136 --> 00:02:04.216 align:middle
And if so, how do we get it?

00:02:05.316 --> 00:02:09.366 align:middle
To find out, move over to your terminal
and run another bin/console command:

00:02:09.706 --> 00:02:19.256 align:middle
php bin/console debug:autowiring Say hello to
one of the most powerful bin/console commands.

00:02:19.596 --> 00:02:20.936 align:middle
I love this thing!

00:02:21.526 --> 00:02:25.186 align:middle
This lists all of the services
that exist in our app.

00:02:25.716 --> 00:02:28.066 align:middle
Okay, it's actually not the full list,

00:02:28.546 --> 00:02:31.776 align:middle
but this shows the services
that you're most likely to need.

00:02:31.776 --> 00:02:35.756 align:middle
And even though our app is small,
there's a lot of stuff here!

00:02:36.316 --> 00:02:38.096 align:middle
There's a filesystem service...

00:02:38.436 --> 00:02:40.686 align:middle
and down here a cache service.

00:02:41.136 --> 00:02:43.096 align:middle
There's even a twig service!

00:02:43.866 --> 00:02:45.806 align:middle
Is there a service for logging?

00:02:46.486 --> 00:02:47.676 align:middle
You can look in this list...

00:02:48.036 --> 00:02:52.936 align:middle
or you can re-run this command and
search for the word log: Excellent!

00:02:53.546 --> 00:02:56.296 align:middle
For now, ignore everything
except for the first line.

00:02:56.526 --> 00:03:00.026 align:middle
This line tells us that there
is a logger service

00:03:00.406 --> 00:03:06.346 align:middle
and that this object implements an
interface called Psr\Log\LoggerInterface.

00:03:07.366 --> 00:03:10.726 align:middle
Ok, so why does knowing that help us?

00:03:11.546 --> 00:03:18.626 align:middle
Because if you want a service, you ask for it
by using the type-hint shown in this command.

00:03:19.096 --> 00:03:20.936 align:middle
It's called autowiring.

00:03:21.806 --> 00:03:22.326 align:middle
Let's try it.

00:03:22.736 --> 00:03:26.066 align:middle
Head over to our controller
and add a second argument.

00:03:26.516 --> 00:03:29.576 align:middle
Actually, the order of these
arguments doesn't matter.

00:03:30.046 --> 00:03:35.756 align:middle
What matters is that the new argument
is type-hinted with LoggerInterface.

00:03:36.266 --> 00:03:38.336 align:middle
I'll hit tab to autocomplete that...

00:03:38.676 --> 00:03:42.366 align:middle
so that PhpStorm adds the use statement on top.

00:03:42.366 --> 00:03:47.096 align:middle
In this case, the argument can
be called anything, like $logger.

00:03:47.866 --> 00:03:52.876 align:middle
When Symfony sees this type-hint, it looks
inside of the debug:autowiring list...

00:03:53.296 --> 00:03:57.626 align:middle
and because there's a match, it
will pass us the logger service.

00:03:58.276 --> 00:04:03.456 align:middle
So we now know two different types of arguments
that we are allowed to have in controller:

00:04:04.226 --> 00:04:08.446 align:middle
you can have an argument whose name
matches a wildcard in the route

00:04:08.756 --> 00:04:13.796 align:middle
or an argument whose type-hint matches
one of the services in our app.

00:04:14.876 --> 00:04:20.786 align:middle
Ok, so now that we know Symfony will pass
us the logger service object, let's use it!

00:04:21.126 --> 00:04:24.906 align:middle
I don't know, yet, what methods
I can call on it but...

00:04:24.906 --> 00:04:26.536 align:middle
if we say $logger-&gt;...

00:04:27.046 --> 00:04:28.226 align:middle
PhpStorm...

00:04:28.226 --> 00:04:30.056 align:middle
tells us! That was easy!

00:04:30.976 --> 00:04:34.236 align:middle
I'm going to log something
at an info() priority level.

00:04:34.776 --> 00:04:39.276 align:middle
Let's say: Returning API response
for song And then the $id.

00:04:41.346 --> 00:04:45.276 align:middle
Actually, we can do something even
cooler with this logger service.

00:04:46.406 --> 00:04:49.046 align:middle
Add {song} to the message...

00:04:49.466 --> 00:04:52.726 align:middle
and add a second argument, which is an array

00:04:52.726 --> 00:04:56.216 align:middle
of extra information you want
to attach to the log message.

00:04:57.036 --> 00:04:59.376 align:middle
Pass song set to $id.

00:05:01.136 --> 00:05:07.196 align:middle
In a minute, you'll see that the logger
will print the actual id in place of {song}.

00:05:08.756 --> 00:05:12.196 align:middle
Anyways, this controller
is for our API endpoint.

00:05:12.686 --> 00:05:15.966 align:middle
So let's go over and refresh.

00:05:16.966 --> 00:05:20.746 align:middle
Um... ok! So no error, that's good!

00:05:21.176 --> 00:05:22.536 align:middle
But did it work?

00:05:23.236 --> 00:05:24.596 align:middle
Where does the logger service...

00:05:24.896 --> 00:05:26.436 align:middle
actually log to?

00:05:27.276 --> 00:05:33.246 align:middle
Let's find out next, learn a trick to
see the profiler even for API requests

00:05:33.506 --> 00:05:36.786 align:middle
and then leverage our second service directly.

