WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.176 --> 00:00:06.156 align:middle
It's time to talk about the most
fundamental part of Symfony: services!

00:00:06.906 --> 00:00:12.516 align:middle
Honestly, Symfony is nothing more than a
bunch of useful objects that work together.

00:00:12.516 --> 00:00:18.126 align:middle
For example, there's a router object
that matches routes and generates URLs.

00:00:18.486 --> 00:00:21.026 align:middle
There's a Twig object that renders templates.

00:00:21.456 --> 00:00:25.936 align:middle
And there's a Logger object that
Symfony is already using internally

00:00:26.146 --> 00:00:30.136 align:middle
to store things in a var/log/dev.log file.

00:00:31.286 --> 00:00:35.916 align:middle
Actually, everything in Symfony
- I mean everything -

00:00:36.066 --> 00:00:39.716 align:middle
is done by one of these useful objects.

00:00:39.716 --> 00:00:44.086 align:middle
And these useful objects have
a special name: services.

00:00:44.836 --> 00:00:48.266 align:middle
But don't get too excited
about that word - service.

00:00:48.636 --> 00:00:56.896 align:middle
It's a special word for a really simple
idea: a service is any object that does work,

00:00:57.196 --> 00:01:02.466 align:middle
like generating URLs, sending emails
or saving things to a database.

00:01:03.986 --> 00:01:07.266 align:middle
Symfony comes with a huge number of services,

00:01:07.526 --> 00:01:11.656 align:middle
and I want you to think of
services as your tools.

00:01:12.226 --> 00:01:18.826 align:middle
Like, if I gave you the logger service, or
object, then you could use it to log messages.

00:01:19.346 --> 00:01:23.316 align:middle
If I gave you a mailer service,
you could send some emails!

00:01:23.746 --> 00:01:29.216 align:middle
Tools! The entire second half
of Symfony is all about learning

00:01:29.216 --> 00:01:32.016 align:middle
where to find these services
and how to use them.

00:01:32.836 --> 00:01:36.986 align:middle
Every time you learn about a
new service, you get a new tool,

00:01:37.266 --> 00:01:40.796 align:middle
and become just a little bit more dangerous!

00:01:41.626 --> 00:01:43.306 align:middle
Let's check out the logging system.

00:01:43.306 --> 00:01:52.306 align:middle
Find your terminal and run: tail -f
var/log/dev.log I'll clear the screen.

00:01:53.636 --> 00:01:56.636 align:middle
Now, refresh the page, and move back.

00:01:58.506 --> 00:02:04.246 align:middle
Awesome! This proves that Symfony
has some sort of logging system.

00:02:04.966 --> 00:02:11.236 align:middle
And since everything is done by a
service, there must be a logger object.

00:02:11.236 --> 00:02:15.996 align:middle
So here's the question: how
can we get the logger service

00:02:16.176 --> 00:02:19.766 align:middle
so that we can log our own messages?

00:02:20.736 --> 00:02:26.566 align:middle
Here's the answer: inside the controller,
on the method, add an additional argument.

00:02:27.476 --> 00:02:32.526 align:middle
Give it a LoggerInterface type hint
- hit tab to auto-complete that

00:02:32.916 --> 00:02:36.616 align:middle
and call it whatever you
want, how about $logger.

00:02:37.696 --> 00:02:43.456 align:middle
Remember: when you autocomplete, PhpStorm
adds the use statement to the top for you.

00:02:44.506 --> 00:02:50.836 align:middle
Now, we can use one of its methods:
$logger-&gt;info('Article is being hearted').

00:02:52.806 --> 00:02:55.026 align:middle
Before we talk about this, let's try it!

00:02:55.026 --> 00:02:57.666 align:middle
Find your browser and click the heart.

00:02:58.756 --> 00:03:00.466 align:middle
That hit the AJAX endpoint.

00:03:01.726 --> 00:03:02.696 align:middle
Go back to the terminal.

00:03:03.406 --> 00:03:05.646 align:middle
Yes! There it is at the bottom.

00:03:06.046 --> 00:03:07.736 align:middle
Hit Ctrl+C to exit tail.

00:03:08.906 --> 00:03:10.556 align:middle
Ok cool! But...

00:03:10.636 --> 00:03:13.106 align:middle
how the heck did that work?

00:03:14.206 --> 00:03:20.916 align:middle
Here's the deal: before Symfony executes
our controller, it looks at each argument.

00:03:20.966 --> 00:03:27.226 align:middle
For simple arguments like $slug, it passes
us the wildcard value from the router.

00:03:27.226 --> 00:03:32.996 align:middle
But for $logger, it looks at
the type-hint and realizes

00:03:33.176 --> 00:03:37.526 align:middle
that we want Symfony to pass
us the logger object.

00:03:38.346 --> 00:03:42.026 align:middle
Oh, and the order of the
arguments does not matter.

00:03:42.676 --> 00:03:49.386 align:middle
This is a very powerful idea called
autowiring: if you need a service object,

00:03:49.636 --> 00:03:53.026 align:middle
you just need to know the
correct type-hint to use!

00:03:53.816 --> 00:03:57.756 align:middle
So... how the heck did I
know to use LoggerInterface?

00:03:58.316 --> 00:04:03.476 align:middle
Well, of course, if you look at the official
Symfony docs about the logger, it'll tell you.

00:04:04.076 --> 00:04:06.126 align:middle
But, there's a cooler way.

00:04:07.236 --> 00:04:08.606 align:middle
Go to your terminal and run: .

00:04:08.946 --> 00:04:13.456 align:middle
/bin/console debug:autowiring Boom!

00:04:13.956 --> 00:04:19.686 align:middle
This is a full list of all of the
type-hints that you can use to get a service.

00:04:20.746 --> 00:04:24.896 align:middle
Notice that most of them say that
they are an alias to something.

00:04:25.586 --> 00:04:28.586 align:middle
Don't worry about that too much: like routes,

00:04:28.856 --> 00:04:32.436 align:middle
each service has an internal
name you can use to reference it.

00:04:33.386 --> 00:04:34.926 align:middle
We'll learn more about that later.

00:04:35.666 --> 00:04:41.626 align:middle
Oh, and whenever you install a new package,
you'll get more and more services in this list.

00:04:41.986 --> 00:04:43.296 align:middle
More tools!

00:04:44.066 --> 00:04:44.866 align:middle
And check this out!

00:04:44.866 --> 00:04:50.106 align:middle
If you want to get the Twig service, you
can use either of these two type-hints.

00:04:51.136 --> 00:04:54.966 align:middle
And remember how I said that everything
in Symfony is done by a service?

00:04:55.546 --> 00:05:02.316 align:middle
Well, when we call $this-&gt;render() in
a controller, that's just a shortcut

00:05:02.316 --> 00:05:05.946 align:middle
to fetch the Twig service
and call a method on it.

00:05:05.946 --> 00:05:11.586 align:middle
In fact, let's pretend that the
$this-&gt;render() shortcut does not exist.

00:05:12.276 --> 00:05:13.836 align:middle
How could we render a template?

00:05:14.896 --> 00:05:19.306 align:middle
No problem: we just need the Twig service.

00:05:19.306 --> 00:05:22.486 align:middle
Add a second argument with
an Environment type-hint,

00:05:22.876 --> 00:05:25.936 align:middle
because that's the class name
we saw in debug:autowiring.

00:05:26.776 --> 00:05:28.566 align:middle
Call the arg $twigEnvironment.

00:05:30.046 --> 00:05:36.856 align:middle
Next, change the return statement to
be $html = $twigEnvironment-&gt;render().

00:05:38.056 --> 00:05:42.656 align:middle
The method we want to call on the
Twig object is coincidentally the same

00:05:42.656 --> 00:05:43.866 align:middle
as the controller shortcut.

00:05:44.996 --> 00:05:51.266 align:middle
Then at the bottom, return
new Response() and pass $html.

00:05:51.266 --> 00:05:54.356 align:middle
Ok, this is way more work than before...

00:05:54.546 --> 00:05:57.116 align:middle
and I would not do this in a real project.

00:05:57.636 --> 00:06:03.696 align:middle
But, I wanted to prove a point: when you
use the $this-&gt;render() shortcut method

00:06:03.696 --> 00:06:09.866 align:middle
on the controller, all it really does
is call render() on the Twig service

00:06:09.866 --> 00:06:13.456 align:middle
and then wrap it inside a
Response object for you.

00:06:14.306 --> 00:06:16.926 align:middle
Try it! Go back and refresh the page.

00:06:19.546 --> 00:06:22.136 align:middle
It works exactly like before!

00:06:22.746 --> 00:06:29.176 align:middle
Of course we will use shortcut methods,
because they make our life way more awesome.

00:06:30.216 --> 00:06:32.776 align:middle
I'll change my code back
to look like it did before.

00:06:32.976 --> 00:06:38.616 align:middle
But the point is this: everything
is done by a service.

00:06:39.116 --> 00:06:45.986 align:middle
If you learn to master services, you can
do anything from anywhere in Symfony.

00:06:46.846 --> 00:06:51.946 align:middle
There's a lot more to say about the topic of
services, and so many other parts of Symfony:

00:06:52.546 --> 00:06:59.326 align:middle
configuration, Doctrine &amp; the database,
forms, Security and APIs, to just name a few.

00:07:00.216 --> 00:07:05.586 align:middle
The Space Bar is far from being the galactic
information source that we know it will be!

00:07:06.096 --> 00:07:07.496 align:middle
But, congrats!

00:07:07.716 --> 00:07:11.686 align:middle
You just spent an hour getting
an awesome foundation in Symfony.

00:07:12.216 --> 00:07:17.606 align:middle
You will not regret your hard work: you're
on your way to building great things and,

00:07:17.976 --> 00:07:21.486 align:middle
as always, becoming a better
and better developer.

00:07:22.546 --> 00:07:24.166 align:middle
Alright guys, seeya next time!

