WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.176 --> 00:00:04.706 align:middle
We now have control over where
the user goes after registering.

00:00:05.566 --> 00:00:09.196 align:middle
But... it's not as awesome as it could be.

00:00:09.766 --> 00:00:10.476 align:middle
Let me show you why.

00:00:12.006 --> 00:00:16.796 align:middle
First, look at my app/config/security.yml file.

00:00:16.856 --> 00:00:21.866 align:middle
In order to access any URL that start
with /admin, you need to be logged in.

00:00:22.496 --> 00:00:29.126 align:middle
For example, if I go to /admin/genus,
it sends me to the login page.

00:00:31.236 --> 00:00:36.086 align:middle
Thanks to Symfony's form_login
system, if we logged in,

00:00:36.396 --> 00:00:41.646 align:middle
it would automatically redirect
us back to /admin/genus...

00:00:42.026 --> 00:00:43.496 align:middle
which is awesome!

00:00:43.926 --> 00:00:46.076 align:middle
That's clearly where the user wants to go.

00:00:46.996 --> 00:00:53.026 align:middle
But what if I instead clicked a link
to register and submitted that form?

00:00:53.736 --> 00:00:57.936 align:middle
Shouldn't that also redirect
me back to /admin/genus after?

00:00:59.256 --> 00:01:04.626 align:middle
Yea, that would be way better: I want to
keep the user's experience really smooth.

00:01:04.856 --> 00:01:06.936 align:middle
How can we do this?

00:01:07.676 --> 00:01:08.186 align:middle
Guess what?

00:01:08.596 --> 00:01:16.446 align:middle
It's almost effortless, thanks to a trait that
was added in Symfony 3.1: TargetPathTrait.

00:01:16.606 --> 00:01:20.776 align:middle
In your subscriber, use TargetPathTrait.

00:01:23.696 --> 00:01:26.096 align:middle
Then, down in onRegistrationSuccess,

00:01:26.416 --> 00:01:32.866 align:middle
add $url = $this-&gt;getTargetPath()
- a method provided by that trait.

00:01:33.956 --> 00:01:42.936 align:middle
Pass this $event-&gt;getRequest()-&gt;getSession()
and for the "provider key" argument, pass main.

00:01:44.206 --> 00:01:47.716 align:middle
Provider key is a fancy term
for your firewall's name.

00:01:52.476 --> 00:01:53.416 align:middle
What's going on here?

00:01:54.196 --> 00:02:01.776 align:middle
Well, whenever you try to access a secured page
anonymously, Symfony stores that URL somewhere

00:02:01.776 --> 00:02:05.266 align:middle
in the session before redirecting
you to the login page.

00:02:06.196 --> 00:02:11.536 align:middle
Then form_login uses that to
redirect you after you authenticate.

00:02:12.336 --> 00:02:18.546 align:middle
The TargetPathTrait is just a shortcut for
us to read that same key from the session.

00:02:18.736 --> 00:02:23.906 align:middle
If $url is empty - it means the user
went directly to the registration page.

00:02:24.416 --> 00:02:24.976 align:middle
No worries!

00:02:25.386 --> 00:02:26.816 align:middle
Just send them to the homepage.

00:02:26.916 --> 00:02:30.506 align:middle
Let's try the entire flow.

00:02:30.946 --> 00:02:38.176 align:middle
I'll go back to /admin/genus: it
redirects me to the login page and sets

00:02:38.176 --> 00:02:40.376 align:middle
that session key behind the scenes.

00:02:41.856 --> 00:02:47.586 align:middle
Then, I'll manually type in /register
- but pretend like we clicked a link.

00:02:48.386 --> 00:02:51.766 align:middle
Register as aquanaut6, password turtles.

00:02:54.436 --> 00:03:00.296 align:middle
Booya! Logged in and on the /admin/genus page.

00:03:00.696 --> 00:03:03.086 align:middle
That's a kick butt registration form.

