WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.096 --> 00:00:04.536 align:middle
Hats off for nearly making it
through the first Symfony tutorial.

00:00:04.756 --> 00:00:08.246 align:middle
You've taken a huge step toward
building whatever you want on the web.

00:00:08.786 --> 00:00:13.526 align:middle
To celebrate, I want to play with MakerBundle:
Symfony's awesome tool for code generation.

00:00:13.916 --> 00:00:17.646 align:middle
Let's get it installed: composer
require symfony/maker-bundle --

00:00:17.646 --> 00:00:23.616 align:middle
dev We haven't seen that -- dev flag
yet, but it's not that important.

00:00:24.336 --> 00:00:26.316 align:middle
Move over and open composer.json.

00:00:26.316 --> 00:00:31.096 align:middle
Thanks to the flag, instead
of symfony/maker-bundle going

00:00:31.146 --> 00:00:35.276 align:middle
under the require key, it was
added down here under require-dev.

00:00:36.096 --> 00:00:39.526 align:middle
By default, when you run composer
install, it will download everything

00:00:39.526 --> 00:00:42.546 align:middle
under both require and require-dev.

00:00:43.196 --> 00:00:47.586 align:middle
But require-dev is meant for packages that
don't need to be available on production:

00:00:47.936 --> 00:00:50.786 align:middle
packages that you only need
when you're developing locally.

00:00:51.286 --> 00:00:56.096 align:middle
That's because, when you do deploy, if
you want, you can tell Composer: Hey!

00:00:56.346 --> 00:01:01.576 align:middle
Only install the packages under my require
key: don't install the require-dev stuff.

00:01:01.576 --> 00:01:04.136 align:middle
That can give you a small
performance boost on production.

00:01:04.486 --> 00:01:06.096 align:middle
But mostly, it's not a big deal.

00:01:06.716 --> 00:01:08.666 align:middle
Now, we just installed a bundle.

00:01:08.996 --> 00:01:11.606 align:middle
Do you remember the main
thing that bundles give us?

00:01:11.996 --> 00:01:13.506 align:middle
That's right: services.

00:01:14.016 --> 00:01:18.926 align:middle
This time, the services that MakerBundle gave us
are services that provide new console commands.

00:01:19.286 --> 00:01:20.396 align:middle
Drumroll please.

00:01:21.166 --> 00:01:24.536 align:middle
Run: php bin/console Or, actually,

00:01:24.536 --> 00:01:27.006 align:middle
I'll start running symfony
console, which is the same thing.

00:01:27.006 --> 00:01:31.906 align:middle
Thanks to the new bundle, we have a
ton of commands that start with make!

00:01:32.606 --> 00:01:35.736 align:middle
Commands for generating a security
system, making a controller,

00:01:35.856 --> 00:01:39.176 align:middle
generating doctrine entities
to talk to the database, forms,

00:01:39.176 --> 00:01:40.896 align:middle
listeners, a registration form....

00:01:41.056 --> 00:01:43.306 align:middle
lots and lots of stuff!

00:01:43.306 --> 00:01:46.536 align:middle
Let's use one of these to make
our own custom console command.

00:01:47.056 --> 00:01:52.106 align:middle
Run: symfony console make:command This will
interactively ask us about our command.

00:01:52.396 --> 00:01:54.586 align:middle
Let's call it: app:ship-report.

00:01:55.446 --> 00:02:01.116 align:middle
Done! This created exactly one file:
src/Command/ShipReportCommand.php.

00:02:01.116 --> 00:02:02.086 align:middle
Let's go check that out!

00:02:06.386 --> 00:02:10.856 align:middle
Cool! This is a normal class -
it is a service, by the way -

00:02:11.086 --> 00:02:13.596 align:middle
but with an attribute above: #[AsCommand].

00:02:14.056 --> 00:02:15.906 align:middle
This tells Symfony: Yo!

00:02:15.996 --> 00:02:17.106 align:middle
See this service?

00:02:17.226 --> 00:02:22.536 align:middle
It's not just a service: I would like you to
include it in the list of console commands.

00:02:22.536 --> 00:02:26.186 align:middle
The attribute includes the name
of the command and a description.

00:02:26.626 --> 00:02:31.016 align:middle
Then the class itself has a configure()
method where we can add arguments and options.

00:02:31.626 --> 00:02:36.736 align:middle
But the main part is that, when somebody calls
this command, Symfony will call execute().

00:02:37.326 --> 00:02:39.156 align:middle
This $io variable is cool.

00:02:39.486 --> 00:02:45.136 align:middle
It lets us output things - like $this-&gt;note()
or $this-&gt;success() - with different styles.

00:02:45.356 --> 00:02:50.266 align:middle
And though we don't see it here, we can
also ask the user questions interactively.

00:02:50.786 --> 00:02:51.476 align:middle
The best part?

00:02:51.726 --> 00:02:55.346 align:middle
Just by creating this class, it's ready to use!

00:02:55.346 --> 00:03:01.396 align:middle
Try it out: symfony console
app:ship-report That's so cool!

00:03:01.686 --> 00:03:05.076 align:middle
The message down here comes from the success
message at the bottom of the command.

00:03:05.076 --> 00:03:10.126 align:middle
And thanks to configure(), we
have one argument called arg1.

00:03:10.666 --> 00:03:13.916 align:middle
Arguments are string that we
pass after the command, like:

00:03:13.916 --> 00:03:20.566 align:middle
symfony console app:ship-report ryan It
says: You passed an argument: ryan ...

00:03:20.896 --> 00:03:22.906 align:middle
which comes from this spot in the command.

00:03:23.486 --> 00:03:25.816 align:middle
There are a lot of fun things
you can do with commands...

00:03:26.036 --> 00:03:27.496 align:middle
and I want to play with one of them.

00:03:28.156 --> 00:03:32.566 align:middle
One of the superpowers of the $io object
is to create animated progress bars.

00:03:33.086 --> 00:03:34.726 align:middle
Imagine we're building a ship report...

00:03:34.966 --> 00:03:36.956 align:middle
and it requires some heavy queries.

00:03:36.956 --> 00:03:39.496 align:middle
So we want to show a progress bar on the screen.

00:03:40.156 --> 00:03:44.506 align:middle
To do that, say $io-&gt;progressStart()
and pass it however many rows

00:03:44.506 --> 00:03:46.386 align:middle
of data we're looping through and handling.

00:03:46.856 --> 00:03:50.086 align:middle
Let's pretend we're looping over
100 rows of data for this report.

00:03:50.086 --> 00:03:54.036 align:middle
Instead of looping over real
data, create a fake loop with for.

00:03:55.126 --> 00:03:57.896 align:middle
I'm even going to include the
$i variable in the middle!

00:03:59.226 --> 00:04:04.166 align:middle
Inside, to advance the progress
bar, say $io-&gt;advance().

00:04:04.166 --> 00:04:07.566 align:middle
Then, here is where we would do
our heavy query or heavy work.

00:04:07.566 --> 00:04:11.546 align:middle
Fake that with a usleep(10000)
to create a short pause.

00:04:11.546 --> 00:04:15.616 align:middle
After the loop, finish with
$io-&gt;progressFinish().

00:04:16.326 --> 00:04:16.806 align:middle
That's it!

00:04:16.806 --> 00:04:22.566 align:middle
Spin over and give that a
try: Oh, that is so cool.

00:04:23.186 --> 00:04:24.566 align:middle
And... that's it people!

00:04:24.956 --> 00:04:26.336 align:middle
Give yourself a high five...

00:04:26.336 --> 00:04:29.286 align:middle
or, better, surprise a co-worker
with a jumping high five!

00:04:29.686 --> 00:04:32.736 align:middle
Then celebrate with a well-deserved beer, tea,

00:04:32.986 --> 00:04:35.636 align:middle
walk around the block or
frisbee match with your dog.

00:04:36.086 --> 00:04:36.476 align:middle
Because...

00:04:36.586 --> 00:04:37.286 align:middle
you did it!

00:04:37.546 --> 00:04:40.726 align:middle
You took the first big step into
being dangerous with Symfony.

00:04:41.186 --> 00:04:45.836 align:middle
Then, come back and try this stuff
out: play with it, build a blog,

00:04:45.836 --> 00:04:48.536 align:middle
create a few static pages, anything.

00:04:48.926 --> 00:04:51.226 align:middle
That will make a huge difference.

00:04:51.226 --> 00:04:55.666 align:middle
And if you ever have any questions, we watch
the comment section below each video closely

00:04:55.666 --> 00:04:56.936 align:middle
and answer everything.

00:04:57.556 --> 00:04:59.036 align:middle
Also keep going!

00:04:59.356 --> 00:05:03.566 align:middle
In the next tutorial, we're going to
become even more dangerous by diving deeper

00:05:03.566 --> 00:05:05.756 align:middle
into Symfony's configuration and services:

00:05:06.226 --> 00:05:09.016 align:middle
the systems that drive everything
you'll do in Symfony.

00:05:09.516 --> 00:05:11.036 align:middle
Alright, friends, see you next time!

