WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.086 --> 00:00:02.416 align:middle
Ok, here's the scoop.

00:00:02.846 --> 00:00:07.836 align:middle
Wesley Crusher - everyone's favorite ensign
from Star Trek - has retired from Starfleet

00:00:07.836 --> 00:00:11.906 align:middle
and is working with us to start a
new business: Wesley's Star Shop.

00:00:11.906 --> 00:00:16.376 align:middle
Someone's gotta break the Ferengi monopoly
on the galaxy's starship repair business,

00:00:16.376 --> 00:00:19.116 align:middle
and he's hired us to build the site to do that.

00:00:19.496 --> 00:00:22.336 align:middle
We're about to give the Ferengi
a run for their latinum!

00:00:22.766 --> 00:00:24.676 align:middle
And it starts with building our first page.

00:00:25.086 --> 00:00:27.416 align:middle
The idea behind every page is always the same.

00:00:27.416 --> 00:00:30.136 align:middle
Step one, give it a cool URL.

00:00:30.426 --> 00:00:31.776 align:middle
That's called the route.

00:00:32.226 --> 00:00:35.916 align:middle
Step two, write a PHP function
that generates the page.

00:00:36.246 --> 00:00:37.776 align:middle
That's known as the controller.

00:00:38.256 --> 00:00:42.426 align:middle
And that page could be HTML,
JSON, ASCII art, anything.

00:00:42.946 --> 00:00:47.176 align:middle
In Symfony, the controller is
always a method inside a PHP class.

00:00:47.176 --> 00:00:50.006 align:middle
So, we need to create our first PHP code!

00:00:50.356 --> 00:00:53.146 align:middle
Where does PHP code live in our app?

00:00:53.566 --> 00:00:55.496 align:middle
That's right, the src/ directory.

00:00:56.086 --> 00:00:59.456 align:middle
Inside this src/Controller/
directory, create a new file.

00:01:00.096 --> 00:01:05.566 align:middle
I would normally select new "PHP class", but
for this first time, create an empty file.

00:01:05.966 --> 00:01:07.446 align:middle
We'll do each part by hand.

00:01:07.806 --> 00:01:11.696 align:middle
Call it MainController.php, but you
can name this whatever you want.

00:01:12.386 --> 00:01:16.766 align:middle
Inside, add the open PHP tag, and
then say class MainController.

00:01:18.106 --> 00:01:21.826 align:middle
Above this, add a namespace of App\Controller.

00:01:22.996 --> 00:01:24.626 align:middle
Okay, a few things about this.

00:01:24.766 --> 00:01:29.836 align:middle
First, the fact that I put this class inside
a directory called Controller is optional.

00:01:30.036 --> 00:01:31.636 align:middle
That's just a convention.

00:01:31.806 --> 00:01:34.546 align:middle
You could rename this to
whatever the Klingon word

00:01:34.546 --> 00:01:37.336 align:middle
for Controller is and everything
would the same...

00:01:37.426 --> 00:01:39.306 align:middle
and probably be more interesting!

00:01:39.986 --> 00:01:44.126 align:middle
However, there are a few rules
about PHP classes in general.

00:01:44.616 --> 00:01:50.086 align:middle
The first is that every class must have
a namespace and that namespace needs

00:01:50.086 --> 00:01:51.996 align:middle
to match your directory structure.

00:01:52.426 --> 00:01:56.606 align:middle
It's always going to be App\ then
whatever directory you're inside.

00:01:56.606 --> 00:02:02.076 align:middle
Without going into too much detail, that's
a rule you'll find in every PHP project.

00:02:02.816 --> 00:02:06.996 align:middle
The second rule is that your class
name must match your filename .php.

00:02:06.996 --> 00:02:13.096 align:middle
If you mess either of these up, you'll get an
error from PHP that it can't find your class.

00:02:13.466 --> 00:02:15.676 align:middle
The Ferengi never make this mistake.

00:02:16.436 --> 00:02:19.186 align:middle
Anyway, our goal is to create a controller,

00:02:19.186 --> 00:02:21.946 align:middle
which is a method in a class
that builds the page.

00:02:22.356 --> 00:02:24.656 align:middle
Add a new public function and call it homepage.

00:02:24.976 --> 00:02:26.636 align:middle
But, again, the name doesn't matter.

00:02:27.256 --> 00:02:27.836 align:middle
And... yea!

00:02:28.196 --> 00:02:31.266 align:middle
It's not done yet, but this is our controller!

00:02:31.766 --> 00:02:35.266 align:middle
But remember, a page is the
combination of a controller

00:02:35.266 --> 00:02:38.366 align:middle
and a route, which defines the page's URL.

00:02:38.866 --> 00:02:40.106 align:middle
Where do we put the route?

00:02:40.466 --> 00:02:44.856 align:middle
Right above the controller method using
a feature of PHP called an attribute.

00:02:45.246 --> 00:02:51.996 align:middle
Write #[] then start typing Route with a
capital R. Check out that auto-completion!

00:02:52.516 --> 00:02:57.966 align:middle
Either option will work, but use the one from
Attribute - which is newer - then hit tab.

00:02:58.616 --> 00:03:01.366 align:middle
When I did that, something
super important happened:

00:03:01.756 --> 00:03:05.466 align:middle
my editor added a use statement
at the top of the class.

00:03:05.466 --> 00:03:09.966 align:middle
Anytime you use a PHP attribute, you
must have a corresponding use statement

00:03:10.046 --> 00:03:11.886 align:middle
for it in the same file.

00:03:12.356 --> 00:03:17.356 align:middle
These attributes work almost like PHP
functions: you can pass a bunch of arguments.

00:03:17.726 --> 00:03:19.406 align:middle
The first one is the path.

00:03:19.406 --> 00:03:24.396 align:middle
Set this to /. Thanks to this, when
someone goes to the homepage - / -

00:03:24.396 --> 00:03:28.956 align:middle
Symfony will call this controller
method to build the page!

00:03:29.386 --> 00:03:31.386 align:middle
What... should our method return?

00:03:31.786 --> 00:03:34.056 align:middle
Just the HTML we want, right?

00:03:34.056 --> 00:03:36.126 align:middle
Or the JSON if we're building an API?

00:03:37.156 --> 00:03:40.166 align:middle
Almost. The web works on a well-known system.

00:03:40.436 --> 00:03:43.326 align:middle
First, a user requests a page.

00:03:43.686 --> 00:03:46.386 align:middle
They say: Hey, I want to see /products...

00:03:46.386 --> 00:03:48.866 align:middle
or I want to see /users.json.

00:03:49.426 --> 00:03:53.326 align:middle
What we return back to them,
yes, contains HTML or JSON.

00:03:53.756 --> 00:03:55.146 align:middle
But it's more than that.

00:03:55.546 --> 00:04:00.136 align:middle
We also communicate back a status code -
which says whether the response was okay

00:04:00.136 --> 00:04:05.996 align:middle
or had an error - as well as these things called
headers, which communicate a bit more info,

00:04:06.186 --> 00:04:08.256 align:middle
like the format of what we're returning.

00:04:08.766 --> 00:04:12.356 align:middle
This whole beautiful package
is called the response.

00:04:12.396 --> 00:04:16.816 align:middle
So yeah, most of the time, we'll just be
thinking about returning HTML or JSON.

00:04:16.816 --> 00:04:22.556 align:middle
But what we're truly sending is this
bigger, nerdier thing called a response.

00:04:22.556 --> 00:04:26.946 align:middle
And so our entire job as web developers - no
matter what language we're programming in -

00:04:27.096 --> 00:04:31.476 align:middle
is to understand the request from the
user, then create and return the response.

00:04:31.886 --> 00:04:34.926 align:middle
And this brings us back to
something I love about Symfony.

00:04:35.436 --> 00:04:36.996 align:middle
What does our controller return?

00:04:37.346 --> 00:04:39.796 align:middle
A new Response object from Symfony!

00:04:40.076 --> 00:04:45.736 align:middle
And again, PhpStorm wants to auto-complete this,
suggesting a few different Response classes.

00:04:46.296 --> 00:04:49.546 align:middle
We want the one from the
Symfony HttpFoundation component.

00:04:50.026 --> 00:04:54.606 align:middle
That's the Symfony library that contains
everything related to requests &amp; responses.

00:04:55.076 --> 00:05:01.896 align:middle
Hit tab. Once again, when we did that, PhpStorm
added a use statement at the top of the file.

00:05:02.366 --> 00:05:04.626 align:middle
I'm going to use this trick constantly.

00:05:05.086 --> 00:05:09.356 align:middle
Anytime you reference a class name, you
must have a corresponding use statement,

00:05:09.356 --> 00:05:12.776 align:middle
else PHP will give you an error that
it can't find the Response class.

00:05:13.356 --> 00:05:17.216 align:middle
Inside this, the first argument is
the content that we want to return.

00:05:17.216 --> 00:05:19.306 align:middle
Start with a hardcoded string.

00:05:22.386 --> 00:05:23.366 align:middle
Route, check!

00:05:23.556 --> 00:05:25.746 align:middle
Controller that returns a Response, check!

00:05:25.956 --> 00:05:27.256 align:middle
Let's try this.

00:05:27.766 --> 00:05:33.096 align:middle
Back at the browser, this page was just a demo
that shows before we have a real homepage.

00:05:33.596 --> 00:05:35.666 align:middle
Now that we do, when we refresh...

00:05:36.096 --> 00:05:37.066 align:middle
there it is!

00:05:37.516 --> 00:05:42.336 align:middle
I know it's not much yet, but we just learned
the first fundamental part of Symfony:

00:05:42.656 --> 00:05:44.926 align:middle
that every page is a route &amp; controller...

00:05:45.156 --> 00:05:48.856 align:middle
and that every controller returns a response.

00:05:48.856 --> 00:05:53.456 align:middle
Oh, and it's optional, but because our
controller always returns a Response,

00:05:53.726 --> 00:05:56.046 align:middle
we can add a Response return type.

00:05:56.696 --> 00:06:01.216 align:middle
That doesn't change how our code works,
but it makes it more descriptive to read.

00:06:01.216 --> 00:06:05.366 align:middle
And if we ever did something silly and
returned something other than a response,

00:06:05.366 --> 00:06:07.626 align:middle
PHP would give us a clear reminder.

00:06:08.686 --> 00:06:14.306 align:middle
Next up: to supercharge our development, let's
install our first third-party package and learn

00:06:14.306 --> 00:06:16.736 align:middle
about Symfony's amazing recipe system.

