WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.156 --> 00:00:02.356 align:middle
We can override templates.

00:00:02.546 --> 00:00:04.206 align:middle
We can override translations.

00:00:04.416 --> 00:00:05.866 align:middle
We can override forms.

00:00:06.166 --> 00:00:07.796 align:middle
But there's more than that.

00:00:08.306 --> 00:00:10.906 align:middle
For example, after we finish registration,

00:00:11.146 --> 00:00:14.466 align:middle
we're redirected to this
registration confirmation page.

00:00:15.046 --> 00:00:15.536 align:middle
You know what?

00:00:15.936 --> 00:00:19.686 align:middle
I'd rather do something else:
I'd rather redirect

00:00:19.826 --> 00:00:23.086 align:middle
to the homepage and skip this page entirely.

00:00:24.136 --> 00:00:24.986 align:middle
How can we do that?

00:00:25.796 --> 00:00:28.306 align:middle
Well, let's do a little bit of digging.

00:00:28.976 --> 00:00:31.866 align:middle
If you hover over the route
name in the web debug toolbar,

00:00:32.186 --> 00:00:35.986 align:middle
you can see that this is page
rendered by RegistrationController.

00:00:36.896 --> 00:00:42.186 align:middle
Back in my editor I'll press Shift+Shift and
look for RegistrationController in the bundle.

00:00:42.926 --> 00:00:46.406 align:middle
Specifically, registerAction() is responsible

00:00:46.406 --> 00:00:52.136 align:middle
for both rendering the registration
page and processing the form submit.

00:00:53.736 --> 00:00:59.346 align:middle
And check this out: after the form is valid,
it redirects to the confirmation page.

00:01:01.036 --> 00:01:05.676 align:middle
So at first, it seems like we need
to override the controller itself.

00:01:06.486 --> 00:01:07.446 align:middle
But not so fast!

00:01:08.066 --> 00:01:15.396 align:middle
The controller - in fact every controller
in FOSUserBundle is littered with events:

00:01:16.456 --> 00:01:22.996 align:middle
REGISTRATION_INITIALIZE, REGISTRATION_SUCCESS,
REGISTRATION_COMPLETED and REGISTRATION_FAILURE.

00:01:24.206 --> 00:01:28.626 align:middle
Each of these represents a hook
point where we can add custom logic.

00:01:28.806 --> 00:01:32.886 align:middle
In this case, if you look closely, you can see

00:01:32.886 --> 00:01:38.726 align:middle
that after it dispatches an event
called REGISTRATION_SUCCESS, below,

00:01:39.186 --> 00:01:43.606 align:middle
it checks to see if the $event
has a response set on it.

00:01:45.876 --> 00:01:49.456 align:middle
If it does not, it redirects
to the confirmation page.

00:01:50.086 --> 00:01:53.606 align:middle
But if it does, it uses that response.

00:01:54.876 --> 00:01:56.306 align:middle
That's the key!

00:01:57.086 --> 00:02:00.746 align:middle
If we can add a listener
to REGISTRATION_SUCCESS,

00:02:01.396 --> 00:02:04.306 align:middle
we can create our own RedirectResponse

00:02:04.546 --> 00:02:07.976 align:middle
and set that on the event so
that this controller uses it.

00:02:09.346 --> 00:02:14.236 align:middle
Let's go! Inside of AppBundle, create
a new directory called EventListener.

00:02:17.016 --> 00:02:22.906 align:middle
And in there, a new PHP class: how about
RedirectAfterRegistrationSubscriber.

00:02:25.376 --> 00:02:28.616 align:middle
Make this implement EventSubscriberInterface:

00:02:29.216 --> 00:02:32.006 align:middle
the interface that all event
subscribers must have.

00:02:33.246 --> 00:02:37.176 align:middle
I'll use our favorite Code-&gt;Generate
menu, or Command+N on a Mac,

00:02:37.796 --> 00:02:41.386 align:middle
go to "Implement Methods" and
select getSubscribedEvents.

00:02:44.876 --> 00:02:51.956 align:middle
We want to attach a listener to
FOSUserEvents::REGISTRATION_SUCCESS, which,

00:02:51.956 --> 00:02:56.586 align:middle
by the way, is just a constant
that equals some string event name.

00:02:58.006 --> 00:02:59.396 align:middle
In getSubscribedEvents(),

00:02:59.576 --> 00:03:07.616 align:middle
add FOSUserEvents::REGISTRATION_SUCCESS
assigned to onRegistrationSuccess.

00:03:09.436 --> 00:03:13.076 align:middle
This means that when the
REGISTRATION_SUCCESS event is fired,

00:03:13.336 --> 00:03:16.556 align:middle
the onRegistrationSuccess
method should be called.

00:03:17.716 --> 00:03:21.546 align:middle
Create that above: public
function onRegistrationSuccess().

00:03:22.476 --> 00:03:25.746 align:middle
Oh, and notice that when
this event is dispatched,

00:03:26.056 --> 00:03:29.106 align:middle
the bundle passes a FormEvent object.

00:03:30.356 --> 00:03:35.976 align:middle
That will be the first argument to
our listener method: FormEvent $event.

00:03:36.646 --> 00:03:39.096 align:middle
That's what we need to set the response onto.

00:03:40.776 --> 00:03:46.256 align:middle
Before we go any further, I'll hold command
and click into the FOSUserEvents class...

00:03:46.256 --> 00:03:48.026 align:middle
cause it's awesome!

00:03:49.316 --> 00:03:54.616 align:middle
It holds a list of every event dispatched
by FOSUserBundle, what its purpose is,

00:03:54.826 --> 00:03:57.466 align:middle
and what event object you will receive.

00:03:58.046 --> 00:04:00.206 align:middle
This is gold.

00:04:03.776 --> 00:04:09.716 align:middle
Back in onRegistrationSuccess, we need to create
a RedirectResponse and set it on the event.

00:04:10.646 --> 00:04:13.656 align:middle
But to redirect to the homepage,
we'll need the router.

00:04:13.776 --> 00:04:19.636 align:middle
At the top of the class, create
public function __construct()

00:04:20.576 --> 00:04:23.066 align:middle
with a RouterInterface $router argument.

00:04:23.156 --> 00:04:30.746 align:middle
Next, I'll hit Option+Enter, select
"Initialize Fields" and choose router.

00:04:30.856 --> 00:04:36.446 align:middle
That was just a shortcut to create the
private $router property and set it

00:04:36.446 --> 00:04:38.416 align:middle
in the constructor: nothing fancy.

00:04:39.886 --> 00:04:48.446 align:middle
Now, in onRegistrationSuccess() we can say
$url = $this-&gt;router-&gt;generate('homepage'),

00:04:48.716 --> 00:04:55.266 align:middle
and $response = new RedirectResponse($url).

00:04:56.676 --> 00:04:59.416 align:middle
You may or may not be familiar
with RedirectResponse.

00:05:00.096 --> 00:05:05.076 align:middle
In a controller, to redirect,
you use $this-&gt;redirectToRoute().

00:05:06.066 --> 00:05:09.806 align:middle
In reality, that's just a
shortcut for these two lines!

00:05:10.786 --> 00:05:14.106 align:middle
Finally, add $event-&gt;setResponse($response).

00:05:16.706 --> 00:05:19.706 align:middle
Ok, this class is perfect!

00:05:21.036 --> 00:05:25.476 align:middle
To tell Symfony about the event
subscriber, head to app/config/services.yml.

00:05:25.476 --> 00:05:33.506 align:middle
At the bottom, add
app.redirect_after_registration_subscriber,

00:05:34.396 --> 00:05:39.526 align:middle
set the class, and add autowire: true.

00:05:40.806 --> 00:05:44.276 align:middle
By doing that, thanks to the
RouterInterface type-hint,

00:05:44.646 --> 00:05:47.396 align:middle
Symfony will automatically
know to pass us the router.

00:05:49.406 --> 00:05:56.206 align:middle
Finally, add a tag on the bottom:
name: kernel.event_subscriber.

00:05:56.756 --> 00:05:58.976 align:middle
And we are done!

00:05:59.976 --> 00:06:01.146 align:middle
Try it out!

00:06:01.726 --> 00:06:09.636 align:middle
Go back to /register and
signup as aquanaut5@gmail.com.

00:06:09.746 --> 00:06:13.066 align:middle
Fill out the rest of the fields and submit!

00:06:14.446 --> 00:06:17.336 align:middle
Boom! Back to our homepage!

00:06:18.166 --> 00:06:21.246 align:middle
You can customize just about
anything with events.

00:06:21.836 --> 00:06:23.566 align:middle
So don't override the controller.

00:06:23.966 --> 00:06:25.926 align:middle
Instead, hook into an event!

