WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:00.056 --> 00:00:08.126 align:middle
The most popular bundle in all of Symfony is...

00:00:08.616 --> 00:00:10.326 align:middle
GifExceptionBundle!

00:00:11.176 --> 00:00:12.206 align:middle
Wait... that's not right...

00:00:12.616 --> 00:00:13.886 align:middle
but it should be.

00:00:14.406 --> 00:00:19.446 align:middle
The actual most popular bundle
is, of course, FOSUserBundle.

00:00:20.496 --> 00:00:28.016 align:middle
And it's easy to know why: it gives you a lot of
crazy-cool stuff, out-of-the-box: registration,

00:00:28.486 --> 00:00:32.146 align:middle
reset password, change password,
and edit profile pages.

00:00:32.146 --> 00:00:34.576 align:middle
Honestly, it feels like stealing.

00:00:35.416 --> 00:00:36.676 align:middle
But guess what?

00:00:37.326 --> 00:00:42.096 align:middle
A lot of smart devs don't like FOSUserBundle.

00:00:42.706 --> 00:00:43.616 align:middle
How could that be?

00:00:44.976 --> 00:00:48.436 align:middle
The bundle does give you a lot of stuff.

00:00:48.476 --> 00:00:52.496 align:middle
But, it's not a magician: it doesn't
know what your design looks like,

00:00:52.706 --> 00:00:59.176 align:middle
what fields your registration form should have,
the clever text you want in your emails or where

00:00:59.176 --> 00:01:01.986 align:middle
to redirect the user after registration.

00:01:02.076 --> 00:01:06.646 align:middle
See, there's a lot of stuff
that makes your site special.

00:01:07.686 --> 00:01:12.316 align:middle
And that means that if you're going to
use FOSUserBundle - which is awesome -

00:01:12.696 --> 00:01:16.056 align:middle
you're going to need to customize
a lot of things.

00:01:16.496 --> 00:01:19.036 align:middle
And you've come to the right
place: once we're done,

00:01:19.386 --> 00:01:23.776 align:middle
you're going to be embarrissingly
good extending this bundle.

00:01:23.776 --> 00:01:29.516 align:middle
I mean, your co-workers will gaze at you
in amazement as you hook into events,

00:01:29.516 --> 00:01:32.126 align:middle
customize text and override templates.

00:01:32.126 --> 00:01:33.956 align:middle
It's going to be beautiful thing.

00:01:34.626 --> 00:01:35.736 align:middle
Let's do it!

00:01:36.836 --> 00:01:40.056 align:middle
As always, you should totally
code along with me...

00:01:40.426 --> 00:01:42.146 align:middle
it's probably what the cool kids are doing.

00:01:42.406 --> 00:01:46.306 align:middle
Just click the download button
on this page and unzip that guy.

00:01:49.476 --> 00:01:53.816 align:middle
Inside, you'll find a start/ directory
that has the same code you see here.

00:01:54.326 --> 00:01:57.176 align:middle
Open up README.md for hilarious text...

00:01:57.656 --> 00:01:58.836 align:middle
and setup details.

00:02:00.046 --> 00:02:06.426 align:middle
The last step will be to find your favorite
terminal and run: php bin/console server:run

00:02:06.706 --> 00:02:08.816 align:middle
to start the built-in PHP web server.

00:02:09.536 --> 00:02:14.426 align:middle
Ok, load this up in your
browser: http://localhost:8000.

00:02:15.556 --> 00:02:17.436 align:middle
Welcome to AquaNote!

00:02:17.556 --> 00:02:19.816 align:middle
This is the same project we've been building

00:02:19.816 --> 00:02:24.176 align:middle
in our main Symfony tutorials,
but without any security logic.

00:02:24.526 --> 00:02:27.466 align:middle
Gasp! See that Login link?

00:02:27.866 --> 00:02:28.656 align:middle
It's a lie!

00:02:29.016 --> 00:02:30.336 align:middle
It goes nowhere!

00:02:30.756 --> 00:02:32.736 align:middle
The login link is a lie!

00:02:34.346 --> 00:02:39.426 align:middle
Google for the FOSUserBundle
documentation: it lives right on Symfony.com.

00:02:40.216 --> 00:02:43.516 align:middle
And make sure you're on the 2.0 version.

00:02:44.486 --> 00:02:46.046 align:middle
We'll go through the install details...

00:02:46.096 --> 00:02:47.956 align:middle
but in our own order.

00:02:48.606 --> 00:02:51.916 align:middle
Of course, first, copy the
composer require line...

00:02:52.066 --> 00:02:53.916 align:middle
but don't worry about the version number.

00:02:54.916 --> 00:02:56.976 align:middle
Head over to your terminal and run that:

00:02:58.306 --> 00:03:02.266 align:middle
composer require friendsofsymfony/user-bundle
While we're waiting for Jordi to prepare

00:03:02.266 --> 00:03:07.076 align:middle
out delicious FOSUserBundle package, go back and
copy the new FOSUserBundle() line from the docs,

00:03:10.606 --> 00:03:16.096 align:middle
open our app/AppKernel.php file
and paste it to enable the bundle.

00:03:16.926 --> 00:03:22.226 align:middle
Oh, and FOSUserBundle uses
SwiftmailerBundle to send the password reset

00:03:22.496 --> 00:03:24.566 align:middle
and registration confirmation emails.

00:03:25.096 --> 00:03:26.666 align:middle
So, uncomment that.

00:03:26.846 --> 00:03:34.046 align:middle
You can also create your own custom mailer
or tell FOSUserBundle to not send emails.

00:03:35.456 --> 00:03:36.826 align:middle
Ok, flip back to your terminal.

00:03:37.356 --> 00:03:39.596 align:middle
Bah! It exploded!

00:03:40.906 --> 00:03:43.686 align:middle
Ok, it did install FOSUserBundle.

00:03:43.836 --> 00:03:45.766 align:middle
So, let's not panic people.

00:03:46.366 --> 00:03:51.906 align:middle
It just went crazy while trying to clear
the cache: The child node db_driver

00:03:52.006 --> 00:03:59.786 align:middle
at path fos_user must be configured Ah, so
this bundle has some required configuration.

00:04:01.276 --> 00:04:08.006 align:middle
Before we fill that in, I have a question:
what does FOSUserBundle actually give us?

00:04:08.826 --> 00:04:16.326 align:middle
In reality, just 2 things: a User entity and
a bunch of routes and controllers for things

00:04:16.326 --> 00:04:22.086 align:middle
like registration, edit password, reset
password, profile and a login page.

00:04:25.406 --> 00:04:27.496 align:middle
To use the User class from the bundle,

00:04:27.856 --> 00:04:32.196 align:middle
we need to create our own small
User class that extends their's.

00:04:33.146 --> 00:04:38.386 align:middle
Inside src/AppBundle/Entity,
create a new PHP class called User.

00:04:40.976 --> 00:04:44.986 align:middle
To extend the base class, add a
use statement for their User class

00:04:45.286 --> 00:04:50.306 align:middle
with as BaseUser to avoid a lame conflict.

00:04:51.606 --> 00:04:53.966 align:middle
Then add, extends BaseUser.

00:04:55.346 --> 00:05:00.696 align:middle
There's just one thing we must do in
this class: add a protected $id property.

00:05:01.656 --> 00:05:05.386 align:middle
Beyond that, this is just a normal entity class.

00:05:06.426 --> 00:05:12.546 align:middle
So I'll go to the Code-&gt;Generate menu - or
Command+N on a Mac - and choose ORM Class

00:05:12.866 --> 00:05:15.896 align:middle
to get my fancy @ORM\Entity stuff on top.

00:05:17.056 --> 00:05:21.516 align:middle
Add ticks around the user table name -
that's a keyword in some database engines.

00:05:22.776 --> 00:05:28.516 align:middle
Now, go back to Code-&gt;Generate, choose
ORM Annotation and select the id column.

00:05:29.876 --> 00:05:32.406 align:middle
Boom! We are annotated!

00:05:32.566 --> 00:05:37.006 align:middle
Finally, go back to Code-&gt;Generate
one last time...

00:05:37.466 --> 00:05:41.966 align:middle
until we do it more later - and
generate the getId() method.

00:05:43.176 --> 00:05:45.326 align:middle
This class is done!

00:05:46.186 --> 00:05:49.466 align:middle
And now we have everything we need
to add the required configuration.

00:05:50.376 --> 00:05:59.616 align:middle
In the docs, scroll down a little: under the
Configure section, copy their example config.

00:06:00.086 --> 00:06:05.716 align:middle
Then, back in your editor,
open app/config/config.yml,

00:06:06.196 --> 00:06:07.636 align:middle
and paste this down at the bottom.

00:06:09.236 --> 00:06:15.026 align:middle
Ok, The db_driver is orm and the
firewall_name - main - is also correct.

00:06:15.586 --> 00:06:17.816 align:middle
You can see that key in security.yml.

00:06:19.606 --> 00:06:22.596 align:middle
And yea, the user_class is also correct.

00:06:23.846 --> 00:06:25.336 align:middle
We're crushing it!

00:06:25.496 --> 00:06:35.136 align:middle
For the email stuff, it doesn't matter, use
hello@aquanote.com and AquaNote Postman.

00:06:36.536 --> 00:06:40.156 align:middle
Finally, our app should be un-broken!

00:06:40.776 --> 00:06:45.876 align:middle
Try the console: php bin/console It's alive!

00:06:46.656 --> 00:06:50.236 align:middle
Now, we can generate the
migration for our User class:

00:06:50.366 --> 00:06:57.406 align:middle
php bin/console doctrine:migrations:diff
Yep, that looks about right.

00:06:59.736 --> 00:07:07.236 align:middle
Run it: php bin/console
doctrine:migrations:migrate Perfect!

00:07:07.376 --> 00:07:13.666 align:middle
At this point, the only thing this bundle
has given us is the base User class...

00:07:13.986 --> 00:07:20.166 align:middle
which is nice, but nothing too special, it has
a bunch of properties like username, email,

00:07:20.446 --> 00:07:26.556 align:middle
password, lastLogin, etc. The second
thing this bundle gives us is a bunch

00:07:26.556 --> 00:07:28.366 align:middle
of free routes and controllers.

00:07:29.026 --> 00:07:33.266 align:middle
But to get those, we need to import the routes.

00:07:33.266 --> 00:07:37.256 align:middle
Back in the documentation, scroll
down a bit until you see step 6:

00:07:37.706 --> 00:07:39.936 align:middle
Import FOSUserBundle routing files.

00:07:40.046 --> 00:07:43.636 align:middle
Ah ha! Copy that routing import.

00:07:43.776 --> 00:07:48.756 align:middle
Find your app/config/routing.yml
file and paste that on top.

00:07:48.756 --> 00:07:55.296 align:middle
As soon as you do that, we have new routes!

00:07:55.356 --> 00:08:02.136 align:middle
At your terminal, check them out:
php bin/console debug:router Awesome!

00:08:03.836 --> 00:08:11.236 align:middle
We have /login, /profile/edit, /register and
others for resetting and changing your password.

00:08:13.776 --> 00:08:18.826 align:middle
If we manually go to /register in the browser...

00:08:19.516 --> 00:08:21.706 align:middle
yea! A functional registration form.

00:08:22.676 --> 00:08:27.196 align:middle
I know, it's horribly, embarrassingly
ugly: we'll fix that.

00:08:28.056 --> 00:08:30.916 align:middle
Oh, and back on the docs,
all the way at the bottom,

00:08:31.256 --> 00:08:33.806 align:middle
there's a page about Advanced
routing configuration.

00:08:34.486 --> 00:08:35.606 align:middle
Open that in a new tab.

00:08:36.946 --> 00:08:42.186 align:middle
In your app, you may not need all of
the pages that FOSUserBundle gives you.

00:08:43.166 --> 00:08:48.496 align:middle
Maybe you need registration and reset
password, but you don't need a profile page.

00:08:49.056 --> 00:08:49.706 align:middle
No problem!

00:08:50.276 --> 00:08:55.676 align:middle
Instead of importing this all.xml file,
just import the specific routes you want.

00:08:57.146 --> 00:09:03.336 align:middle
Seriously feel free to do this: if some route
or controller isn't helping you, kill it.

00:09:04.646 --> 00:09:07.526 align:middle
Oh, and if your registration
page doesn't look mine -

00:09:07.716 --> 00:09:11.506 align:middle
if it has some weird keys
instead of real text, don't worry.

00:09:12.106 --> 00:09:18.336 align:middle
In app/config/config.yml, just make sure to
uncomment the translator key under framework.

00:09:18.466 --> 00:09:24.206 align:middle
FOSUserBundle uses the translator
to translate internal "keys"

00:09:24.576 --> 00:09:27.576 align:middle
into English or whatever other language.

00:09:28.676 --> 00:09:29.766 align:middle
And basically...

00:09:30.066 --> 00:09:30.936 align:middle
at this point...

00:09:31.386 --> 00:09:33.286 align:middle
we're done installing the bundle!

00:09:34.096 --> 00:09:35.246 align:middle
But how is that possible?

00:09:35.656 --> 00:09:38.516 align:middle
We haven't touched anything related to security!

00:09:39.416 --> 00:09:44.266 align:middle
Here's the truth: this bundle has
almost nothing to do with security:

00:09:44.926 --> 00:09:48.676 align:middle
it just gives you a User class
and some routes &amp; controllers!

00:09:49.336 --> 00:09:56.326 align:middle
We could already register, reset our password or
edit our profile without doing any more setup.

00:09:57.696 --> 00:10:04.286 align:middle
Well, that's almost true: we do need a tiny
bit of security to make registration work.

00:10:05.316 --> 00:10:14.286 align:middle
In security.yml, add an encoders key
with AppBundle\Entity\User set to bcrypt.

00:10:15.706 --> 00:10:21.766 align:middle
When we register, FOSUserBundle needs to encode
the plain-text password before saving it.

00:10:22.386 --> 00:10:24.636 align:middle
This tells it what algorithm to use.

00:10:26.136 --> 00:10:28.546 align:middle
There's one other small bit
of security we need right now.

00:10:30.976 --> 00:10:38.246 align:middle
In the documentation, under
step 4, copy the providers key.

00:10:39.136 --> 00:10:42.466 align:middle
Paste that over the old providers
key in security.yml.

00:10:42.466 --> 00:10:48.466 align:middle
I'll talk about this more in a few minutes,
but it's needed for registration only

00:10:48.466 --> 00:10:53.516 align:middle
because FOSUserBundle logs
us in after registering...

00:10:53.876 --> 00:10:55.426 align:middle
which is really nice!

00:10:55.576 --> 00:10:59.506 align:middle
In the next chapter, we'll
do more security setup.

00:10:59.696 --> 00:11:02.286 align:middle
But for right now, that's all we need!

00:11:02.946 --> 00:11:04.866 align:middle
So try it out!

00:11:06.106 --> 00:11:07.576 align:middle
Refresh /register.

00:11:09.546 --> 00:11:18.956 align:middle
Let's use aquanaut1@gmail.com,
aquanaut1, and password turtles.

00:11:22.786 --> 00:11:28.066 align:middle
And boom! We are registered and logged in!

00:11:30.336 --> 00:11:33.166 align:middle
We even have a role: ROLE_USER.

00:11:33.896 --> 00:11:34.786 align:middle
More on that later.

00:11:36.276 --> 00:11:43.056 align:middle
With a User class, a route import and a tiny
bit of security, everything in the bundle works:

00:11:43.426 --> 00:11:47.746 align:middle
registration, reset password,
edit password and edit profile.

00:11:48.876 --> 00:11:51.756 align:middle
The only thing you can't do is log out...

00:11:52.126 --> 00:11:58.846 align:middle
or login. But that's not FOSUserBundle's
fault: setting up security is our job.

00:11:59.486 --> 00:12:00.266 align:middle
Let's do it next.

