WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.146 --> 00:00:05.876 align:middle
Ok! The first half of Symfony:
route-controller-response is in the books!

00:00:06.506 --> 00:00:11.286 align:middle
The second half is all about useful objects.

00:00:11.286 --> 00:00:15.276 align:middle
Obviously, returning a string response
like this is not going to take us very far.

00:00:15.816 --> 00:00:17.826 align:middle
Our aquanauts demand more!

00:00:18.456 --> 00:00:22.036 align:middle
In real life, we might need to
render a template, query a database,

00:00:22.036 --> 00:00:24.546 align:middle
or even turn objects into JSON for an API!

00:00:24.836 --> 00:00:31.246 align:middle
Symfony comes with many, optional, useful
objects to help out with stuff like this.

00:00:31.376 --> 00:00:33.746 align:middle
For example, want to send an email?

00:00:33.876 --> 00:00:35.706 align:middle
Symfony has an object for that.

00:00:35.706 --> 00:00:37.216 align:middle
How about log something?

00:00:37.636 --> 00:00:38.596 align:middle
There's an object for that.

00:00:39.506 --> 00:00:43.296 align:middle
These objects are commonly called
services, and that's important:

00:00:43.836 --> 00:00:47.336 align:middle
when you hear the word service,
just think "useful object".

00:00:48.116 --> 00:00:49.596 align:middle
To keep track of all of these services,

00:00:49.866 --> 00:00:53.206 align:middle
Symfony puts them into one big
associative array called the container.

00:00:53.826 --> 00:00:57.326 align:middle
Each object has a key - like mailer or logger.

00:00:58.166 --> 00:01:02.466 align:middle
And to be more honest with you -
sorry, I do like to lie temporarily -

00:01:02.966 --> 00:01:04.776 align:middle
the container is actually an object.

00:01:05.366 --> 00:01:09.406 align:middle
But think of it like an array: each
useful object has an associated key.

00:01:09.506 --> 00:01:15.906 align:middle
If I give you the container, you can ask for the
logger service and it'll give you that object.

00:01:16.936 --> 00:01:19.276 align:middle
The second half of Symfony is all about finding

00:01:19.276 --> 00:01:21.926 align:middle
out what objects are available
and how to use them.

00:01:22.846 --> 00:01:27.116 align:middle
Heck, we'll even add our own service
objects to the container before too long.

00:01:27.446 --> 00:01:29.126 align:middle
That's when things get really cool.

00:01:29.916 --> 00:01:36.226 align:middle
The first useful object is the templating
service: it renders Twig templates.

00:01:36.296 --> 00:01:40.266 align:middle
To get access to the service container, you
need to extend Symfony's base controller.

00:01:41.106 --> 00:01:45.076 align:middle
In GenusController, add extends
Controller from FrameworkBundle.

00:01:45.476 --> 00:01:52.246 align:middle
Hit tab to autocomplete and get the use
statement: To get the templating service,

00:01:52.366 --> 00:01:57.236 align:middle
add $templating =
$this-&gt;container-&gt;get('templating'):

00:01:57.236 --> 00:02:01.926 align:middle
The container pretty much
only has one method: get.

00:02:02.626 --> 00:02:06.366 align:middle
Give it the nickname to the service
and it will return that object.

00:02:06.366 --> 00:02:07.536 align:middle
It's super simple.

00:02:09.036 --> 00:02:12.596 align:middle
Quickly, open the var directory,
right click on the cache directory

00:02:12.676 --> 00:02:14.926 align:middle
and click "mark this directory as excluded".

00:02:15.636 --> 00:02:17.326 align:middle
Symfony caches things...

00:02:17.526 --> 00:02:21.186 align:middle
that's not important yet, but
excluding this is important:

00:02:21.596 --> 00:02:24.016 align:middle
this directory confuses autocompletion.

00:02:24.736 --> 00:02:27.886 align:middle
Now type $this-&gt;container-&gt;get('templating').

00:02:28.506 --> 00:02:30.166 align:middle
Well hey autocompletion!

00:02:31.286 --> 00:02:32.726 align:middle
With the templating object we can...

00:02:33.086 --> 00:02:34.416 align:middle
well... render a template!

00:02:35.196 --> 00:02:40.186 align:middle
Add $html = $templating-&gt;render('')
followed by the name of the template.

00:02:40.186 --> 00:02:46.616 align:middle
This could be anything, but let's
be logical: genus/show.html.twig.

00:02:47.326 --> 00:02:48.846 align:middle
I'll show you where this lives in a second:

00:02:49.846 --> 00:02:52.036 align:middle
We'll also want to pass some
variables into the template.

00:02:52.626 --> 00:02:55.836 align:middle
Pass a name variable into
Twig that's set to $genusName.

00:02:55.966 --> 00:02:59.616 align:middle
Finally, what do we always
do in Symfony controllers?

00:03:00.096 --> 00:03:02.826 align:middle
We always return a Symfony's Response object.

00:03:02.936 --> 00:03:10.166 align:middle
Stick, that $html into the response object
and return it: Ok, where do templates live?

00:03:10.706 --> 00:03:15.756 align:middle
Ah, it's so simple: templates
live in app/Resources/views.

00:03:16.656 --> 00:03:23.336 align:middle
The one we're looking for will be in
app/Resources/views/genus/show.html.twig.

00:03:24.016 --> 00:03:27.776 align:middle
The existing index.html.twig template
was for the original homepage.

00:03:28.586 --> 00:03:30.786 align:middle
Check it out if you want to, then delete it!

00:03:32.306 --> 00:03:38.066 align:middle
Create a new genus directory and
then a new file: show.html.twig.

00:03:38.956 --> 00:03:40.276 align:middle
Welcome to Twig!

00:03:40.696 --> 00:03:41.736 align:middle
You'll love it.

00:03:42.536 --> 00:03:49.466 align:middle
Add an tag with The Genus and then {{
name }} to print the name variable.

00:03:50.086 --> 00:03:52.466 align:middle
More on Twig in a second: But that's it!

00:03:52.956 --> 00:03:53.816 align:middle
Refresh the browser.

00:03:54.416 --> 00:03:56.296 align:middle
Check out that sweet h1 tag.

00:03:57.616 --> 00:04:03.726 align:middle
Now back up: we just did something
really cool: used our first service.

00:04:04.456 --> 00:04:09.206 align:middle
We now know that rendering a template isn't
done by some deep, dark part of Symfony:

00:04:09.636 --> 00:04:11.466 align:middle
it's done by the templating object.

00:04:11.466 --> 00:04:17.936 align:middle
In fact, Symfony doesn't really do anything:
everything is done by one of these services.

