WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.036 --> 00:00:03.456 align:middle
Alright, remember this hardcoded quote?

00:00:03.976 --> 00:00:08.576 align:middle
Let's create a new class that can replace this
with a random Samuel L Jackson quote instead.

00:00:10.836 --> 00:00:13.806 align:middle
In AppBundle I'll create a
new directory called Service.

00:00:15.406 --> 00:00:19.306 align:middle
Inside of our new Service directory let's
add a php class called QuoteGenerator,

00:00:19.826 --> 00:00:22.076 align:middle
and look how nicely it added
that namespace for us!

00:00:22.076 --> 00:00:28.046 align:middle
Let's get to work by adding
public function getRandomQuote().

00:00:30.736 --> 00:00:35.586 align:middle
I'll paste in some quotes, then we
can use $key = array_rand($quotes);

00:00:35.736 --> 00:00:46.556 align:middle
to get one of those quotes and return it:
Next, I want to register this as a service

00:00:46.556 --> 00:00:48.216 align:middle
and use it inside of my controller.

00:00:48.216 --> 00:00:56.886 align:middle
So hit Shift+Command+O, search for services.yml,
delete the comments under the services key,

00:00:57.016 --> 00:00:59.116 align:middle
and put our service name there instead.

00:00:59.116 --> 00:01:03.406 align:middle
I'll give it a nickname of quote_generator:

00:01:03.406 --> 00:01:07.086 align:middle
Notice that PHPStorm is autocompleting
my tabs wrong,

00:01:07.226 --> 00:01:09.676 align:middle
I want to hit tab and have
it give me four spaces.

00:01:09.676 --> 00:01:12.346 align:middle
So let's fix that real quick in preferences

00:01:12.346 --> 00:01:15.726 align:middle
by first hitting command+,,
then searching for "tab".

00:01:15.726 --> 00:01:23.756 align:middle
In the left tree find yml under "Code
Style" and update the indent from 2 to 4.

00:01:25.056 --> 00:01:27.576 align:middle
Click apply, then ok and that should do it!

00:01:28.786 --> 00:01:30.246 align:middle
Yep, that looks perfect.

00:01:30.696 --> 00:01:34.406 align:middle
As I was saying: we'll call
the service, quote_generator,

00:01:34.776 --> 00:01:36.116 align:middle
but this name really doesn't matter.

00:01:37.076 --> 00:01:41.016 align:middle
And of course we need the class key,
and we have autocomplete here too.

00:01:41.016 --> 00:01:46.156 align:middle
If you hit Control+Space you'll get a
list of all the different keys you can use

00:01:46.156 --> 00:01:49.666 align:middle
to determine how a service is
created, which is pretty incredible.

00:01:49.806 --> 00:01:54.896 align:middle
So we'll do class, but don't
type the whole long name!

00:01:55.526 --> 00:02:00.226 align:middle
Like everywhere else, just type the
last part of the class: here Quote

00:02:00.606 --> 00:02:02.106 align:middle
and hit tab to get the full line.

00:02:02.926 --> 00:02:06.526 align:middle
Now add an empty arguments line:
we don't have any of those yet:

00:02:07.186 --> 00:02:09.536 align:middle
This is now ready to be used in MovieController.

00:02:09.996 --> 00:02:15.766 align:middle
Use Command+Shift+] to move over to that tab.

00:02:15.766 --> 00:02:19.296 align:middle
And here instead of this
quote, we'll say $this-&gt;get('')

00:02:19.296 --> 00:02:24.826 align:middle
and the plugin is already smart enough to know
that the quote_generator service is there.

00:02:24.826 --> 00:02:30.466 align:middle
And it even knows that there is a
method on it called getRandomQuote():

00:02:31.416 --> 00:02:34.256 align:middle
This is one my favorite features
of the Symfony plugin.

00:02:34.886 --> 00:02:38.036 align:middle
Save that, head back to the form, refresh

00:02:39.126 --> 00:02:42.236 align:middle
and now we see a new random
quote at the bottom of the page.

00:02:44.006 --> 00:02:47.486 align:middle
Now in QuoteGenerator, let's pretend
like we need access to some service -

00:02:47.486 --> 00:02:49.876 align:middle
like maybe we want to log
something inside of here.

00:02:50.846 --> 00:02:53.846 align:middle
The normal way of doing that
is with dependency injection,

00:02:54.276 --> 00:02:57.376 align:middle
where we pass the logger
through via the constructor.

00:02:57.746 --> 00:03:00.926 align:middle
So let's do exactly that, but
with as little work as possible.

00:03:00.926 --> 00:03:09.116 align:middle
I could type public function __construct, but
instead I'm going to use the generate command.

00:03:09.536 --> 00:03:12.916 align:middle
Hit Command+N and pick the
"Constructor" option from the menu here.

00:03:12.986 --> 00:03:18.716 align:middle
I don't think this constructor comment is
all that helpful, so go back into preferences

00:03:18.716 --> 00:03:25.246 align:middle
with Command+,, search for "templates",
and under "file and code templates",

00:03:25.246 --> 00:03:27.146 align:middle
we have one called "PHP Constructors".

00:03:27.766 --> 00:03:31.816 align:middle
I'll just go in here and delete
the comment from the template.

00:03:31.816 --> 00:03:33.766 align:middle
Ok, let's try adding the constructor again.

00:03:37.136 --> 00:03:47.476 align:middle
Much cleaner: At this point we need the logger,
so add the argument LoggerInterface $logger:

00:03:48.586 --> 00:03:52.696 align:middle
This is the point where we would normally
create the private $logger property above,

00:03:52.886 --> 00:03:57.036 align:middle
and set it down in the constructor
with $this-&gt;logger = $logger;.

00:03:58.086 --> 00:04:02.396 align:middle
This is a really common task, so if we can find
a faster way to do this that would be awesome.

00:04:03.036 --> 00:04:08.706 align:middle
Time to go back to the actions shortcut,
Option+Enter, select "Initialize fields",

00:04:08.956 --> 00:04:13.946 align:middle
then choose logger, and it adds all that
code for you: We don't even have to feed it!

00:04:14.066 --> 00:04:18.816 align:middle
Farther down, it's really easy to use,
$this-&gt;logger-&gt;info('Selected quote: '.

00:04:20.286 --> 00:04:27.016 align:middle
$quote);: We've added the
argument here, so now we need to go

00:04:27.016 --> 00:04:29.366 align:middle
to services.yml, which I'll move over to.

00:04:29.986 --> 00:04:34.766 align:middle
And notice it's highlighting quote_generator
with a missing argument message because it knows

00:04:34.766 --> 00:04:36.386 align:middle
that this service has one argument.

00:04:37.146 --> 00:04:42.116 align:middle
So we can say @logger, or even Command+O and
then use autocomplete to help us: Head back,

00:04:42.116 --> 00:04:49.496 align:middle
refresh, it still works and I could go down here
to click into my profiler to check out the logs.

00:04:50.416 --> 00:04:53.486 align:middle
Or, in PhpStorm, we can go to
the bottom right Symfony menu,

00:04:53.876 --> 00:04:56.866 align:middle
click it and use the shortcut
to get into the Profiler.

00:04:57.156 --> 00:05:03.446 align:middle
Click that, go to logs, and
there's our quote right there.

00:05:04.416 --> 00:05:06.836 align:middle
The autocompletion of the
services and the ability

00:05:06.836 --> 00:05:10.796 align:middle
to generate your properties is probably one
of the most important features that you need

00:05:10.796 --> 00:05:16.546 align:middle
to master with PHPStorm because it's going
to help you fly when you develop in Symfony.

