WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.066 --> 00:00:04.936 align:middle
When we switched to asynchronous email
sending, we broke our email links!

00:00:05.206 --> 00:00:08.296 align:middle
It's using localhost as our
domain, weird and wrong.

00:00:08.876 --> 00:00:12.596 align:middle
Back in our app, we can get a hint as to
what's going on by looking at the profiler

00:00:12.596 --> 00:00:14.696 align:middle
for the request that sent the email.

00:00:15.246 --> 00:00:17.496 align:middle
Remember, our email is now marked as "queued".

00:00:18.036 --> 00:00:21.996 align:middle
Go to the "Messages" tab and find
the message: SendEmailMessage.

00:00:22.346 --> 00:00:24.596 align:middle
Inside is the TemplatedEmail object.

00:00:24.936 --> 00:00:25.636 align:middle
Open this up.

00:00:26.186 --> 00:00:26.836 align:middle
Interesting!

00:00:27.236 --> 00:00:31.066 align:middle
htmlTemplate is our Twig
template but html is null!

00:00:31.426 --> 00:00:34.176 align:middle
Shouldn't that be set to the
rendered HTML from that template?

00:00:34.796 --> 00:00:38.666 align:middle
This little detail is important:
the email template is not rendered

00:00:38.666 --> 00:00:40.966 align:middle
when our controller sends
the message to the queue.

00:00:41.406 --> 00:00:45.496 align:middle
Nope! the template isn't rendered until
later, when we run messenger:consume.

00:00:46.036 --> 00:00:46.936 align:middle
Why does this matter?

00:00:47.336 --> 00:00:53.596 align:middle
Well messenger:consume is a CLI command, and
when generating absolute URLs in the CLI,

00:00:53.786 --> 00:00:58.796 align:middle
Symfony doesn't know what the domain should
be (or if it should be http or https).

00:00:59.366 --> 00:01:00.906 align:middle
So why does it when in a controller?

00:01:01.396 --> 00:01:04.766 align:middle
In a controller, Symfony uses the
current request to figure this out.

00:01:05.396 --> 00:01:10.296 align:middle
In a CLI command, there is no request so
it gives up and uses http://localhost.

00:01:10.296 --> 00:01:12.026 align:middle
Let's just tell it what the domain should be.

00:01:12.496 --> 00:01:16.736 align:middle
Back in our IDE, open up
config/packages/routing.yaml.

00:01:17.566 --> 00:01:21.526 align:middle
Under framework, routing, these
comments explain this exact issue.

00:01:22.046 --> 00:01:24.626 align:middle
Uncomment default_uri and set it

00:01:24.626 --> 00:01:29.936 align:middle
to https://universal-travel.com -
our lawyers are close to a deal!

00:01:30.706 --> 00:01:34.236 align:middle
In development though, we need to
use our local dev server's URL.

00:01:34.696 --> 00:01:41.616 align:middle
For me, this is 127.0.0.1:8000 but this
might be different for other team members.

00:01:41.906 --> 00:01:45.676 align:middle
I know that Bob uses bob.is.awesome:8000
and he kinda is.

00:01:46.346 --> 00:01:48.296 align:middle
To make this configurable, there's a trick:

00:01:48.756 --> 00:01:52.016 align:middle
the Symfony CLI server sets a
special environment variable

00:01:52.246 --> 00:01:56.866 align:middle
with the domain called
SYMFONY_PROJECT_DEFAULT_ROUTE_URL.

00:01:57.526 --> 00:02:05.996 align:middle
Back in our routing config, add a new section:
when@dev:, framework:, router:, default_uri:

00:02:06.266 --> 00:02:14.106 align:middle
and set it to
%env(SYMFONY_PROJECT_DEFAULT_ROUTE_URL)%:

00:02:14.656 --> 00:02:19.366 align:middle
This environment variable will only be
available if the Symfony CLI server is running

00:02:19.636 --> 00:02:23.636 align:middle
and you're running commands via
symfony console (not bin/console).

00:02:24.146 --> 00:02:27.476 align:middle
To avoid an error if the variable
is missing, set a default.

00:02:27.896 --> 00:02:30.916 align:middle
Still under when@dev, add parameters:

00:02:31.166 --> 00:02:38.536 align:middle
with env(SYMFONY_PROJECT_DEFAULT_ROUTE_URL):
set to http://localhost.

00:02:39.296 --> 00:02:43.096 align:middle
This is Symfony's standard way to set a
default value for an environment variable.

00:02:43.856 --> 00:02:44.636 align:middle
Testing time!

00:02:44.856 --> 00:02:46.566 align:middle
But first, jump back to your terminal.

00:02:47.126 --> 00:02:49.186 align:middle
Because we made some changes to our config,

00:02:49.456 --> 00:02:54.456 align:middle
we need to restart the messenger:consume
command to, sort of, reload our app: Cool!

00:02:54.616 --> 00:02:58.076 align:middle
The command is running again and
using our sweet new Symfony config.

00:02:58.596 --> 00:02:59.536 align:middle
Head back to our app...

00:02:59.746 --> 00:03:00.516 align:middle
and book a trip!

00:03:05.476 --> 00:03:06.796 align:middle
Quickly go back to the terminal...

00:03:07.226 --> 00:03:09.146 align:middle
and we can see the message was processed.

00:03:09.626 --> 00:03:11.036 align:middle
Pop over to Mailtrap and...

00:03:12.026 --> 00:03:12.746 align:middle
here it is!

00:03:13.226 --> 00:03:15.206 align:middle
Moment of truth: click a link...

00:03:15.856 --> 00:03:17.666 align:middle
Sweet, it's working again!

00:03:17.976 --> 00:03:19.196 align:middle
Bob will be so happy!

00:03:19.776 --> 00:03:23.946 align:middle
If you're like me, you probably find having
to keep this messenger:consume command running

00:03:23.946 --> 00:03:26.066 align:middle
in a terminal during development a drag.

00:03:26.546 --> 00:03:30.866 align:middle
Plus, having to restart it every time you
make a code or config change is annoying.

00:03:31.116 --> 00:03:31.756 align:middle
I'm annoyed!

00:03:32.316 --> 00:03:36.026 align:middle
Time to add the fun back to our
functions with another Symfony CLI trick!

00:03:36.646 --> 00:03:41.136 align:middle
In your IDE, open this .symfony.local.yaml file.

00:03:41.656 --> 00:03:44.386 align:middle
This is the Symfony CLI server
config for our app.

00:03:44.786 --> 00:03:45.936 align:middle
See this workers key?

00:03:46.276 --> 00:03:49.996 align:middle
It lets us define processes to run in
the background when we start the server.

00:03:50.466 --> 00:03:52.336 align:middle
We already have the tailwind command set.

00:03:52.926 --> 00:03:53.796 align:middle
Add another worker.

00:03:54.316 --> 00:04:01.166 align:middle
Call it messenger - though that could be
anything - and set cmd to ['symfony', 'console',

00:04:01.886 --> 00:04:06.726 align:middle
'messenger:consume', 'async']:
This solves the issue of needing

00:04:06.726 --> 00:04:08.766 align:middle
to keep this running in a
separate terminal window.

00:04:09.166 --> 00:04:11.876 align:middle
But what about restarting the
command when we make changes?

00:04:12.306 --> 00:04:13.166 align:middle
No problemo!

00:04:13.496 --> 00:04:19.106 align:middle
Add a watch key and set it to
config, src, templates and vendor:

00:04:19.696 --> 00:04:23.336 align:middle
If any files in these directories
change, the worker will restart itself.

00:04:23.596 --> 00:04:28.956 align:middle
Smart! Back in your terminal, restart
the server with symfony server:stop

00:04:30.416 --> 00:04:36.376 align:middle
and symfony serve -d messenger:consume
should be running in the background!

00:04:36.896 --> 00:04:42.036 align:middle
To prove it, run: symfony
server:status 3 workers running!

00:04:42.476 --> 00:04:46.596 align:middle
The actual PHP webserver, the
existing tailwind:build worker,

00:04:46.926 --> 00:04:48.696 align:middle
and our new messenger:consume.

00:04:49.006 --> 00:04:54.766 align:middle
So cool! Next, let's explore how to make
assertions about emails in our functional tests!

