WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:00.036 --> 00:00:03.266 align:middle
Symfony is really two parts...

00:00:03.486 --> 00:00:05.316 align:middle
and we've already learned one of them.

00:00:05.316 --> 00:00:08.686 align:middle
The first part is the route
and controller system.

00:00:09.196 --> 00:00:12.096 align:middle
And I hope you're feeling pretty
comfortable: create a route,

00:00:12.376 --> 00:00:15.816 align:middle
it executes a controller
function, we return a response.

00:00:16.316 --> 00:00:21.916 align:middle
The second half of Symfony is all
about the many "useful objects"

00:00:22.016 --> 00:00:24.596 align:middle
that are floating around inside Symfony.

00:00:24.596 --> 00:00:31.106 align:middle
For example, when we render a template, what
we're actually doing is taking advantage

00:00:31.176 --> 00:00:34.876 align:middle
of a twig object and asking it to render.

00:00:35.376 --> 00:00:39.156 align:middle
The render() method is just a
shortcut to use that object.

00:00:39.746 --> 00:00:46.326 align:middle
There is also a logger object, a cache object
and many more, like a database connection object

00:00:46.486 --> 00:00:51.186 align:middle
and an object that helps make
HTTP requests to other APIs.

00:00:51.826 --> 00:00:52.706 align:middle
Basically...

00:00:52.926 --> 00:00:57.096 align:middle
every single thing that Symfony
does - or that we do -

00:00:57.396 --> 00:01:01.516 align:middle
is actually done by one of these useful objects.

00:01:01.876 --> 00:01:07.756 align:middle
Heck, even the router is an object that figures
out which route matches the current request.

00:01:08.306 --> 00:01:13.206 align:middle
In the Symfony world - well, really, in
the object-oriented programming world -

00:01:13.516 --> 00:01:18.296 align:middle
these "objects that do work" are
given a special name: services.

00:01:18.686 --> 00:01:24.356 align:middle
But don't let that confuse you: when
you hear "service", just think: Hey!

00:01:24.706 --> 00:01:29.316 align:middle
That's an object that does some
work - like a logger object

00:01:29.316 --> 00:01:31.786 align:middle
or a database object that makes queries.

00:01:32.946 --> 00:01:36.026 align:middle
Inside CommentController, let's log something.

00:01:36.556 --> 00:01:40.306 align:middle
To do that work, we need the "logger" service.

00:01:40.706 --> 00:01:41.576 align:middle
How can we get it?

00:01:41.576 --> 00:01:49.506 align:middle
Find your terminal and run: php bin/console
debug:autowiring Say hello to one

00:01:49.506 --> 00:01:52.416 align:middle
of the most important bin/console commands.

00:01:52.776 --> 00:01:56.766 align:middle
This gives us a list of all
the service objects in our app.

00:01:57.176 --> 00:02:01.686 align:middle
Well, ok, this isn't all of
them: but it is a full list

00:02:01.686 --> 00:02:04.616 align:middle
of all the services that you are likely to need.

00:02:05.206 --> 00:02:07.916 align:middle
Even in our small app, there's
a lot of stuff in here:

00:02:08.316 --> 00:02:12.326 align:middle
there's something called
Psr\Log\LoggerInterface,

00:02:12.716 --> 00:02:14.906 align:middle
there's stuff for caching and plenty more.

00:02:15.476 --> 00:02:19.086 align:middle
As we install more bundles, this list will grow.

00:02:19.646 --> 00:02:22.456 align:middle
More services, means more tools.

00:02:22.456 --> 00:02:26.996 align:middle
To find which service allows
us to "log" things, run:

00:02:26.996 --> 00:02:32.786 align:middle
php bin/console debug:autowiring log
This returns a bunch of things...

00:02:33.116 --> 00:02:37.616 align:middle
but ignore all of these down here
for now and focus on the top line.

00:02:38.116 --> 00:02:41.726 align:middle
This tells us that there
is a logger service object

00:02:41.726 --> 00:02:47.686 align:middle
and its class implements
some Psr\Log\LoggerInterface.

00:02:48.146 --> 00:02:50.096 align:middle
Why is that important?

00:02:50.546 --> 00:02:57.366 align:middle
Because if you want the logger service,
you ask for it by using this type-hint.

00:02:57.366 --> 00:02:59.526 align:middle
It's called "autowiring".

00:02:59.976 --> 00:03:02.626 align:middle
Here's how you get a service
from inside a controller.

00:03:03.156 --> 00:03:07.386 align:middle
Add a third argument to your method -
though the argument order doesn't matter.

00:03:07.916 --> 00:03:15.496 align:middle
Say LoggerInterface - auto-complete the one
from Psr\Log\LoggerInterface - and $logger.

00:03:16.936 --> 00:03:21.556 align:middle
This added a use statement above the
class for Psr\Log\LoggerInterface,

00:03:21.816 --> 00:03:27.106 align:middle
which matches the type-hint that
debug:autowiring told us to use.

00:03:27.206 --> 00:03:31.246 align:middle
Thanks to this type-hint, when
Symfony renders our controller,

00:03:31.586 --> 00:03:36.906 align:middle
it will know that we want the logger
service to be passed to this argument.

00:03:37.486 --> 00:03:43.336 align:middle
So... yea: there are now two types of arguments
that you can add to your controller method.

00:03:43.436 --> 00:03:48.996 align:middle
First, you can have an argument whose
name matches a wildcard in your route.

00:03:49.466 --> 00:03:54.586 align:middle
And second, you can have an argument
whose type-hint matches one of the class

00:03:54.586 --> 00:03:57.836 align:middle
or interface names listed in debug:autowiring.

00:03:58.476 --> 00:04:02.646 align:middle
CacheInterface is another type-hint
we could use to get a caching service.

00:04:02.726 --> 00:04:05.956 align:middle
So... let's use this object!

00:04:06.486 --> 00:04:08.816 align:middle
What methods can we call on it?

00:04:08.816 --> 00:04:09.756 align:middle
I have no idea!

00:04:10.146 --> 00:04:14.946 align:middle
But because we properly type-hinted
the argument, we can say $logger-&gt;

00:04:14.946 --> 00:04:19.496 align:middle
and PhpStorm tells us exactly
what methods it has.

00:04:20.076 --> 00:04:23.536 align:middle
Let's use $logger-&gt;info() to say "Voting up!".

00:04:24.116 --> 00:04:26.636 align:middle
Copy that and say "Voting down!"

00:04:26.636 --> 00:04:27.326 align:middle
on the else.

00:04:28.646 --> 00:04:29.546 align:middle
Testing time!

00:04:30.256 --> 00:04:31.766 align:middle
Refresh the page and...

00:04:32.156 --> 00:04:34.116 align:middle
let's click up, down, up.

00:04:35.506 --> 00:04:38.226 align:middle
It... at least doesn't look broken.

00:04:39.106 --> 00:04:42.356 align:middle
Hover over the AJAX part
of the web debug toolbar

00:04:42.636 --> 00:04:45.676 align:middle
and open the profiler for one of these requests.

00:04:46.236 --> 00:04:50.666 align:middle
The profiler has a "Logs"
section , which is the easiest way

00:04:50.666 --> 00:04:53.386 align:middle
to see the log entries for a single request.

00:04:54.076 --> 00:04:54.796 align:middle
There it is!

00:04:55.086 --> 00:04:56.006 align:middle
"Voting up!".

00:04:56.686 --> 00:05:00.286 align:middle
You could also find this in
the var/log/dev.log file.

00:05:00.876 --> 00:05:06.876 align:middle
The point is: Symfony has many, many
useful objects, I mean "services".

00:05:07.416 --> 00:05:11.706 align:middle
And little-by-little, we're going
to start using more of them...

00:05:12.176 --> 00:05:16.956 align:middle
each time by adding a type-hint to
tell Symfony which service we need.

00:05:18.006 --> 00:05:19.666 align:middle
Let's look at one other example.

00:05:20.206 --> 00:05:24.716 align:middle
The first service that we used
in our code was the Twig service.

00:05:25.416 --> 00:05:26.036 align:middle
We used it...

00:05:26.236 --> 00:05:29.816 align:middle
kind of "indirectly" by saying $this-&gt;render().

00:05:30.406 --> 00:05:35.986 align:middle
In reality, that method is a shortcut to
use the Twig service behind the scenes.

00:05:36.596 --> 00:05:38.726 align:middle
And that should not surprise you.

00:05:39.186 --> 00:05:44.536 align:middle
Like I said, everything that's done in
Symfony is actually done by a service.

00:05:45.456 --> 00:05:49.936 align:middle
As a challenge, let's pretend that
the render() function doesn't exist.

00:05:50.406 --> 00:05:54.346 align:middle
Gasp! In the homepage() controller,
comment-out the render() line.

00:05:54.346 --> 00:06:00.336 align:middle
So... how can we use the Twig service
directly to render a template?

00:06:00.726 --> 00:06:01.156 align:middle
I don't know!

00:06:01.156 --> 00:06:04.416 align:middle
We could definitely find some
documentation about this...

00:06:04.846 --> 00:06:09.266 align:middle
but let's see if we can figure
it out by ourselves with the help

00:06:09.456 --> 00:06:15.936 align:middle
of the debug:autowiring command: php
bin/console debug:autowiring twig And, voilà!

00:06:16.576 --> 00:06:21.396 align:middle
There is apparently a class called
Twig\Environment that we can use

00:06:21.396 --> 00:06:24.566 align:middle
as a "type-hint" to get a Twig service.

00:06:24.566 --> 00:06:29.906 align:middle
In our controller, add Environment and
hit tab to add the use statement on top.

00:06:30.336 --> 00:06:32.476 align:middle
I'll call the argument $twigEnvironment.

00:06:33.546 --> 00:06:37.976 align:middle
Inside, add $html = $twigEnvironment-&gt;.

00:06:38.506 --> 00:06:45.536 align:middle
Once again, without reading any documentation,
thanks to the fact that we're coding responsibly

00:06:45.536 --> 00:06:50.856 align:middle
and using type-hints, PhpStorm shows
us all the methods on this class.

00:06:51.416 --> 00:06:54.316 align:middle
Hey! This render() method looks
like it might be what we need!

00:06:54.786 --> 00:06:56.416 align:middle
Pass the same template name as before.

00:06:58.806 --> 00:07:02.606 align:middle
When you use twig directly, instead
of returning a Response object,

00:07:02.816 --> 00:07:05.276 align:middle
it returns a string with the HTML.

00:07:05.866 --> 00:07:14.016 align:middle
No problem: finish with return new Response()
- the one from HttpFoundation - and pass $html.

00:07:14.016 --> 00:07:18.656 align:middle
This is now doing the exact
same thing as $this-&gt;render().

00:07:19.746 --> 00:07:21.566 align:middle
To prove it, click the homepage link.

00:07:23.206 --> 00:07:24.696 align:middle
It still works.

00:07:25.346 --> 00:07:30.566 align:middle
Now in reality, other than being a
"great exercise" to understand services,

00:07:30.946 --> 00:07:34.026 align:middle
there's no reason to do this the long way.

00:07:34.546 --> 00:07:38.226 align:middle
I just want you to understand
that services are really the

00:07:38.456 --> 00:07:40.676 align:middle
"things" doing the work behind the scenes.

00:07:41.026 --> 00:07:46.366 align:middle
And if you want to do something - like log or
render a template - what you really need is

00:07:46.366 --> 00:07:49.726 align:middle
to find out which service does that work.

00:07:50.606 --> 00:07:55.226 align:middle
Trust me, this is the key to unlocking
your full potential in Symfony.

00:07:56.456 --> 00:08:00.536 align:middle
Let's put the old, shorter code back,
and comment out the longer example.

00:08:05.636 --> 00:08:09.476 align:middle
Ok, you've almost made it through
the first Symfony tutorial.

00:08:09.886 --> 00:08:14.726 align:middle
You rock! As a reward, we're going
to finish with something fun:

00:08:14.726 --> 00:08:20.396 align:middle
an introduction into a system called
Webpack Encore that will allow you

00:08:20.396 --> 00:08:23.636 align:middle
to do crazy things with your CSS and JavaScript.

