WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.066 --> 00:00:07.326 align:middle
Symfony is a set of libraries that gives
us tons of tools: tools for logging,

00:00:07.586 --> 00:00:11.336 align:middle
making database queries, sending
emails, rendering templates

00:00:11.336 --> 00:00:14.506 align:middle
and making API calls, just to name a few.

00:00:15.256 --> 00:00:22.436 align:middle
If you counted them, I did, Symfony
consists of about 100 separate libraries.

00:00:22.706 --> 00:00:28.676 align:middle
Wow! Right now, I want to start turning
our pages into true HTML pages...

00:00:28.906 --> 00:00:30.786 align:middle
instead of just returning text.

00:00:31.176 --> 00:00:36.216 align:middle
But we are not going to jam a
bunch of HTML into our PHP classes.

00:00:36.526 --> 00:00:40.216 align:middle
Yuck. Instead, we're going to render a template.

00:00:40.736 --> 00:00:41.396 align:middle
But guess what?

00:00:41.676 --> 00:00:45.106 align:middle
There is no templating library in our project!

00:00:45.806 --> 00:00:50.466 align:middle
What? But I thought you just said that
Symfony has a tool for rendering templates!?

00:00:50.706 --> 00:00:52.876 align:middle
Lies! Well...

00:00:52.876 --> 00:00:54.836 align:middle
Symfony does have a tool for that.

00:00:55.256 --> 00:01:00.046 align:middle
But our app currently uses very
few of the Symfony libraries.

00:01:00.786 --> 00:01:07.076 align:middle
The tools we have so far don't amount to much
more than a route-controller-response system.

00:01:07.976 --> 00:01:11.106 align:middle
If you need to render a template
or make a database query,

00:01:11.326 --> 00:01:14.906 align:middle
we do not have those tools
installed in our app...

00:01:15.306 --> 00:01:18.566 align:middle
yet. I actually love this about Symfony.

00:01:19.206 --> 00:01:26.306 align:middle
Instead of starting us with a gigantic project,
with everything we need, plus tons of stuff

00:01:26.306 --> 00:01:29.366 align:middle
that we don't need, Symfony starts tiny.

00:01:29.766 --> 00:01:33.026 align:middle
Then, if you need something, you install it!

00:01:33.876 --> 00:01:37.616 align:middle
But before we install a templating
library, at your terminal, run:

00:01:37.616 --> 00:01:41.356 align:middle
git status Let's commit everything: git add .

00:01:41.356 --> 00:01:43.016 align:middle
I can safely run git add .

00:01:43.586 --> 00:01:47.416 align:middle
- which adds everything in my directory
to git - because one of the files

00:01:47.416 --> 00:01:53.756 align:middle
that our project originally came with was a
.gitignore file, which already ignores stuff

00:01:53.756 --> 00:01:57.906 align:middle
like the vendor/ directory, var/
directory, and several other paths.

00:01:58.966 --> 00:02:02.196 align:middle
If you're wondering what
these weird marker things are,

00:02:02.376 --> 00:02:06.826 align:middle
that's related to the recipe system,
which we're about to talk about.

00:02:07.926 --> 00:02:13.046 align:middle
Anyways, run git commit and
add a message: Perfect!

00:02:13.266 --> 00:02:15.526 align:middle
And now, we are clean.

00:02:16.546 --> 00:02:20.786 align:middle
Okay. So how can we install
a templating library?

00:02:21.126 --> 00:02:24.976 align:middle
And what templating libraries
are even available for Symfony?

00:02:25.236 --> 00:02:26.916 align:middle
And which is recommended?

00:02:27.476 --> 00:02:32.156 align:middle
Well, of course, a great way to answer
these questions would be check the

00:02:32.156 --> 00:02:33.656 align:middle
Symfony documentation.

00:02:34.176 --> 00:02:35.716 align:middle
But we can also just...

00:02:35.886 --> 00:02:42.546 align:middle
guess! In any PHP project, you can add new
third-party libraries to your app by saying

00:02:42.546 --> 00:02:45.706 align:middle
"composer require" and then the package name.

00:02:46.716 --> 00:02:54.726 align:middle
We don't know the package name we need yet, so
I'll just guess: composer require templates Now,

00:02:54.726 --> 00:02:59.916 align:middle
if you've used Composer before, you might
be screaming at your screen right about now!

00:03:00.446 --> 00:03:06.946 align:middle
Why? Because in Composer, package
names are always something/something.

00:03:06.946 --> 00:03:12.356 align:middle
It is literally not possible to
have a package just named templates.

00:03:12.696 --> 00:03:17.236 align:middle
But watch: when we run this, it works!

00:03:17.586 --> 00:03:23.436 align:middle
And up on top, it says using
version 1 for symfony/twig-pack.

00:03:24.146 --> 00:03:26.996 align:middle
Twig is the name of the templating
engine for Symfony.

00:03:27.956 --> 00:03:31.336 align:middle
To understand this, let's
take a tiny step backwards.

00:03:31.856 --> 00:03:37.776 align:middle
Our project started with a composer.json
file containing several Symfony libraries.

00:03:38.486 --> 00:03:41.766 align:middle
One of these is called symfony/flex.

00:03:42.336 --> 00:03:44.766 align:middle
Flex is a composer plugin.

00:03:45.076 --> 00:03:48.056 align:middle
So it adds more features to composer.

00:03:48.746 --> 00:03:52.076 align:middle
Actually, it adds three superpowers to composer.

00:03:52.746 --> 00:03:56.996 align:middle
The first, which we just
saw, is called Flex aliases.

00:03:56.996 --> 00:04:02.966 align:middle
Head to https://flex.symfony.com to
see a giant page full of packages.

00:04:04.826 --> 00:04:06.206 align:middle
Search for "templates".

00:04:07.546 --> 00:04:08.126 align:middle
Here it is.

00:04:08.276 --> 00:04:15.116 align:middle
Under symfony/twig-pack, it says Aliases:
template, templates, twig, and twig-pack.

00:04:15.956 --> 00:04:19.696 align:middle
The idea between behind Flex
aliases is dead simple.

00:04:20.106 --> 00:04:22.696 align:middle
We type composer require templates.

00:04:23.086 --> 00:04:29.716 align:middle
And then, internally, Flex
changes that to symfony/twig-pack.

00:04:30.556 --> 00:04:34.386 align:middle
Ultimately, that is the package
that Composer installs.

00:04:35.076 --> 00:04:40.526 align:middle
This means that, most of the time, you can
just "composer require" whatever you want,

00:04:40.886 --> 00:04:48.036 align:middle
like composer require logger, composer require
orm, composer require icecream, whatever.

00:04:48.316 --> 00:04:50.246 align:middle
It's just a shortcut system.

00:04:50.936 --> 00:04:57.556 align:middle
The important point is that, what really
got installed was symfony/twig-pack.

00:04:57.976 --> 00:05:01.666 align:middle
And that means that, in our composer.json file,

00:05:01.976 --> 00:05:07.196 align:middle
we should now see symfony/twig-pack
under the require key.

00:05:08.036 --> 00:05:10.886 align:middle
But if you spin over, it's not there!

00:05:11.236 --> 00:05:19.316 align:middle
Gasp! Instead, it added symfony/twig-bundle,
twig/extra-bundle, and twig/twig.

00:05:20.196 --> 00:05:26.166 align:middle
We're witnessing the second superpower
of Symfony Flex: unpacking packs.

00:05:27.106 --> 00:05:29.426 align:middle
Copy the original package name and...

00:05:29.976 --> 00:05:33.316 align:middle
we can actually find that
repository on GitHub by going

00:05:33.316 --> 00:05:38.626 align:middle
to https://github.com/symfony/twig-pack.

00:05:38.626 --> 00:05:43.966 align:middle
And... it contains just one file: composer.json.

00:05:45.236 --> 00:05:51.366 align:middle
And this requires three other packages:
the three we just saw added to our project.

00:05:52.196 --> 00:05:54.706 align:middle
This is called a Symfony pack.

00:05:55.186 --> 00:06:00.636 align:middle
It's... really just a fake package
that helps us install other packages.

00:06:01.316 --> 00:06:05.906 align:middle
It turns out, if you want a rich
template engine to be added to your app,

00:06:06.256 --> 00:06:09.426 align:middle
we recommend installing these three packages.

00:06:09.706 --> 00:06:12.286 align:middle
But instead of making you add them manually,

00:06:12.556 --> 00:06:17.356 align:middle
you can composer require
symfony/twig-pack and get them automatically.

00:06:18.236 --> 00:06:23.626 align:middle
When you install a "pack", like this,
Flex automatically "unpacks" it:

00:06:24.366 --> 00:06:28.176 align:middle
it finds the three packages
that the pack depends on

00:06:28.316 --> 00:06:31.936 align:middle
and adds those into your composer.json file.

00:06:32.556 --> 00:06:37.966 align:middle
So, packs are a shortcut so that you
can run one composer require command

00:06:38.296 --> 00:06:41.656 align:middle
and get multiple libraries
added to your project.

00:06:42.656 --> 00:06:46.746 align:middle
Ok, so what is the third and
final superpower of Flex?

00:06:47.056 --> 00:06:48.286 align:middle
So glad you asked!

00:06:49.026 --> 00:06:54.006 align:middle
To find out, at your terminal,
run: git status Whoa.

00:06:54.596 --> 00:07:00.096 align:middle
Normally when you run composer require,
the only files it should modify -

00:07:00.276 --> 00:07:07.126 align:middle
other than downloading packages into vendor/
- are composer.json and composer.lock.

00:07:07.916 --> 00:07:11.836 align:middle
Flex's third superpower is its recipes system.

00:07:12.506 --> 00:07:17.126 align:middle
Whenever you install a package,
that package may have a recipe.

00:07:17.596 --> 00:07:22.606 align:middle
If it does, in addition to downloading
the package into the vendor/ directory,

00:07:22.776 --> 00:07:25.656 align:middle
Flex will also execute its recipe.

00:07:26.346 --> 00:07:32.526 align:middle
Recipes can do things like add new files
or even modify a few existing files.

00:07:33.036 --> 00:07:39.786 align:middle
Watch: if we scroll up a little, ah
yes: it says "configuring 2 recipes".

00:07:40.276 --> 00:07:44.356 align:middle
So apparently there was a
recipe for symfony/twig-bundle

00:07:44.576 --> 00:07:47.216 align:middle
and also a recipe for twig/extra-bundle.

00:07:47.516 --> 00:07:52.846 align:middle
And these recipes apparently
updated the config/bundles.php file

00:07:53.006 --> 00:07:55.196 align:middle
and added a new directory and file.

00:07:56.366 --> 00:07:59.046 align:middle
The recipe system is sweet.

00:07:59.436 --> 00:08:03.286 align:middle
All we need to do is composer
require a new library

00:08:03.896 --> 00:08:09.556 align:middle
and its recipe will then add all the
configuration files or other setup needed

00:08:09.696 --> 00:08:13.556 align:middle
so that we can start using
that library immediately!

00:08:14.006 --> 00:08:18.326 align:middle
No more following 5 manual
"installation" steps in a README.

00:08:19.036 --> 00:08:22.326 align:middle
When you add a library, it works out-of-the-box.

00:08:23.466 --> 00:08:26.676 align:middle
Next: I want to dive a bit
deeper into the recipes.

00:08:27.146 --> 00:08:28.606 align:middle
Like, where do they live?

00:08:29.026 --> 00:08:30.466 align:middle
What's their favorite color?

00:08:30.656 --> 00:08:34.836 align:middle
And what did this recipe added
specifically to our app and why?

00:08:34.836 --> 00:08:40.416 align:middle
I'm also going to let you in on a little
secret: every file on our project -

00:08:40.706 --> 00:08:43.616 align:middle
all the files in config/,
the public/ directory...

00:08:43.996 --> 00:08:47.286 align:middle
all of this stuff - was added via a recipe.

00:08:47.776 --> 00:08:48.636 align:middle
And I'll prove it.

