WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:00.036 --> 00:00:02.676 align:middle
Hey friends!

00:00:02.676 --> 00:00:08.976 align:middle
Welcome to a new course on upgrading a
Symfony app from Symfony 7 to Symfony 8.

00:00:09.586 --> 00:00:13.526 align:middle
Luckily, newer versions of Symfony
make this upgrade process a breeze.

00:00:13.736 --> 00:00:15.956 align:middle
But there are still some things to
make the upgrade process smoother.

00:00:15.956 --> 00:00:21.356 align:middle
To follow along, download the course code from
the top of this page, open the start directory

00:00:21.356 --> 00:00:23.796 align:middle
in your IDE, and you'll see what I have here.

00:00:23.796 --> 00:00:28.696 align:middle
Follow the README to get everything set up,
when you're ready, in your terminal, run:

00:00:28.826 --> 00:00:34.246 align:middle
symfony server:start -d (the -d flag
runs the server in the background).

00:00:34.816 --> 00:00:37.376 align:middle
Click the link in the output to
open the app in your browser.

00:00:38.096 --> 00:00:39.306 align:middle
Welcome to the Starshop!

00:00:39.766 --> 00:00:43.776 align:middle
If you've followed along with our previous
Symfony 7 courses, this should look familiar.

00:00:44.356 --> 00:00:52.326 align:middle
Down in the web debug toolbar, you can
see we're on Symfony 7.3.5 and PHP 8.3.30.

00:00:52.326 --> 00:00:55.866 align:middle
Our goal? Update this bad boy to Symfony 8!

00:00:55.866 --> 00:01:03.336 align:middle
Since Symfony 8 requires PHP 8.4, a good first
step is to upgrade this app to require PHP 8.4.

00:01:03.756 --> 00:01:08.216 align:middle
At the same time, we'll also upgrade
to the latest version of Symfony 7.3.

00:01:08.216 --> 00:01:12.266 align:middle
If you've done these major upgrades before,
you might be thinking we should upgrade

00:01:12.266 --> 00:01:14.966 align:middle
to 7.4, the last Symfony 7 version.

00:01:15.516 --> 00:01:17.556 align:middle
We will, but I like to take small steps

00:01:17.556 --> 00:01:20.426 align:middle
to avoid getting overwhelmed
with too many changes at once.

00:01:21.146 --> 00:01:23.696 align:middle
In your IDE, open composer.json.

00:01:24.256 --> 00:01:32.246 align:middle
In the require section, change
&gt;=8.3 to &gt;=8.4: This tells composer

00:01:32.246 --> 00:01:35.746 align:middle
at least PHP 8.4 is required for this project.

00:01:36.366 --> 00:01:39.926 align:middle
Scroll down and find the
config.platform.php section.

00:01:40.446 --> 00:01:45.266 align:middle
This is what the Symfony CLI uses to
determine which local version of PHP to use

00:01:45.456 --> 00:01:48.026 align:middle
when you run symfony console or symfony php.

00:01:48.726 --> 00:01:54.326 align:middle
Change this to 8.4: This step isn't required,
but scroll down to the replace section.

00:01:54.906 --> 00:01:59.306 align:middle
This is a list of packages that composer
will ignore when installing dependencies.

00:01:59.796 --> 00:02:02.266 align:middle
You can see it's all polyfill packages.

00:02:02.546 --> 00:02:07.076 align:middle
These are packages that provide features
from newer versions of PHP to older versions.

00:02:07.076 --> 00:02:14.506 align:middle
Since we're on PHP 8.4, we can add the
polyfill packages for 8.3 and 8.4 to this list:

00:02:15.006 --> 00:02:18.436 align:middle
Saves a few bytes of disk space and
a few milliseconds of install time...

00:02:19.306 --> 00:02:22.966 align:middle
Over in our terminal, let's
confirm we're using PHP 8.4 now.

00:02:23.426 --> 00:02:29.766 align:middle
Run: symfony php -- version Nice, 8.4.20!

00:02:30.486 --> 00:02:35.706 align:middle
Now, let's do a composer update to capture all
the changes we made in our composer.json file

00:02:35.886 --> 00:02:38.606 align:middle
and upgrade to the latest allowed dependencies.

00:02:39.136 --> 00:02:46.976 align:middle
Run: symfony composer update No errors, great!

00:02:47.716 --> 00:02:49.696 align:middle
Jump to the browser and refresh the page...

00:02:50.706 --> 00:02:52.806 align:middle
Gross, this is a nasty error.

00:02:53.136 --> 00:02:57.306 align:middle
It doesn't look like the standard Symfony
errors because it couldn't even load Symfony.

00:02:57.766 --> 00:03:00.376 align:middle
This is an error at the composer
autoloader level.

00:03:00.996 --> 00:03:06.456 align:middle
It's saying we're running PHP 8.3
but our dependencies require PHP 8.4.

00:03:07.096 --> 00:03:09.976 align:middle
Remember we ran the Symfony
server in the background earlier?

00:03:10.366 --> 00:03:12.486 align:middle
We ran it with PHP 8.3.

00:03:12.486 --> 00:03:16.426 align:middle
Simple fix, just restart the server with:

00:03:16.426 --> 00:03:25.546 align:middle
symfony server:stop symfony
server:start -d Now refresh the page...

00:03:27.066 --> 00:03:29.036 align:middle
Sweet! We're back in business.

00:03:29.436 --> 00:03:37.146 align:middle
Notice in the web debug toolbar we're
now on Symfony 7.3.11 and PHP 8.4.20.

00:03:37.146 --> 00:03:40.056 align:middle
Now for deprecations!

00:03:40.366 --> 00:03:43.456 align:middle
When Symfony, or PHP itself wants to remove

00:03:43.456 --> 00:03:46.496 align:middle
or change a feature, they
don't just make the change.

00:03:46.796 --> 00:03:49.836 align:middle
This could break tons of apps
and cause a lot of problems.

00:03:50.226 --> 00:03:54.946 align:middle
Instead, they mark the feature as deprecated
for a while, which means it still works

00:03:54.946 --> 00:03:57.176 align:middle
as expected, but it will trigger a warning.

00:03:57.616 --> 00:04:00.276 align:middle
Usually, the warning explains
how to fix the deprecation,

00:04:00.426 --> 00:04:02.736 align:middle
and upgrade to the new way of doing things.

00:04:03.226 --> 00:04:05.696 align:middle
Symfony has a really great deprecation policy.

00:04:06.076 --> 00:04:08.566 align:middle
They guarantee that nothing
will break when upgrading

00:04:08.566 --> 00:04:12.466 align:middle
to a new minor version, like 7.3 to 7.4.

00:04:12.856 --> 00:04:17.266 align:middle
The breaking changes happen in the
major version, like going from 7 to 8.

00:04:17.806 --> 00:04:23.066 align:middle
In 7.4, all breaking changes that will
happen in 8 are marked as deprecations.

00:04:23.436 --> 00:04:25.566 align:middle
This gives you time and guidance to fix them.

00:04:26.086 --> 00:04:29.186 align:middle
You only upgrade to 8 when
no deprecations remain.

00:04:29.186 --> 00:04:31.516 align:middle
So how do we find deprecations!

00:04:31.896 --> 00:04:35.966 align:middle
This deprecation logging panel is going to
be your best friend when upgrading Symfony.

00:04:36.656 --> 00:04:37.796 align:middle
Looks like we have 3.

00:04:38.256 --> 00:04:40.736 align:middle
This first one is a PHP deprecation.

00:04:41.056 --> 00:04:44.836 align:middle
The addDroid() method in our
Starship entity needs a parameter

00:04:44.936 --> 00:04:46.996 align:middle
to be explicitly marked as nullable.

00:04:47.486 --> 00:04:48.716 align:middle
Let's fix that first.

00:04:49.306 --> 00:04:54.756 align:middle
Open src/Entity/Starship.php and
scroll down to the addDroid() method.

00:04:57.646 --> 00:05:00.606 align:middle
Ahh, even PhpStorm is warning me about this.

00:05:01.106 --> 00:05:02.886 align:middle
The fix is simple, add a ?

00:05:02.986 --> 00:05:09.246 align:middle
before DateTimeImmutable: Back to the
browser, refresh the homepage, and...

00:05:09.246 --> 00:05:11.546 align:middle
still 3 deprecations...

00:05:11.626 --> 00:05:12.766 align:middle
It didn't get cleared.

00:05:12.766 --> 00:05:15.426 align:middle
Sometimes when you fix deprecations, you need

00:05:15.426 --> 00:05:18.586 align:middle
to clear the Symfony cache
manually for it to be picked up.

00:05:19.096 --> 00:05:22.196 align:middle
This is because some deprecations
are detected at compile time

00:05:22.386 --> 00:05:23.966 align:middle
when the container is being built.

00:05:24.306 --> 00:05:31.146 align:middle
So in your terminal, run: symfony console
cache:clear Refresh the homepage again...

00:05:32.256 --> 00:05:34.926 align:middle
Whoa, now we have 4 deprecations.

00:05:35.366 --> 00:05:36.106 align:middle
Open the panel.

00:05:36.776 --> 00:05:40.166 align:middle
Our addDroid() deprecation is
gone, so we did fix that one.

00:05:40.166 --> 00:05:44.586 align:middle
Sometimes when you clear the cache
manually, it triggers other deprecations.

00:05:45.256 --> 00:05:47.146 align:middle
Ok, 3 of these are from Doctrine.

00:05:47.326 --> 00:05:51.566 align:middle
We'll have a whole chapter on upgrading Doctrine
a little later, so we'll ignore these for now.

00:05:52.226 --> 00:05:56.106 align:middle
The fourth one is a deprecated
config option from zenstruck/foundry.

00:05:56.686 --> 00:06:00.886 align:middle
Before manually fixing this, let's
first update our Symfony Flex recipes.

00:06:01.256 --> 00:06:04.686 align:middle
Sometimes, just updating these
fixes deprecations for you.

00:06:05.346 --> 00:06:08.146 align:middle
To update the recipes, over
in your terminal, run:

00:06:08.146 --> 00:06:13.496 align:middle
symfony composer recipe:update
Hmm, that didn't work.

00:06:13.826 --> 00:06:15.966 align:middle
It says we have uncommitted changes.

00:06:16.466 --> 00:06:21.296 align:middle
The recipe update command needs to be run
on a clean slate - no pending changes.

00:06:21.846 --> 00:06:24.186 align:middle
Run: git status To see our changes.

00:06:24.776 --> 00:06:28.256 align:middle
composer.json, composer.lock,
and our Starship.php.

00:06:28.256 --> 00:06:31.646 align:middle
Makes sense, let's commit these with:

00:06:31.846 --> 00:06:39.426 align:middle
git commit -a -m "composer update"
The -a adds all modified files

00:06:39.426 --> 00:06:44.146 align:middle
to the changelist before committing
and -m lets us set a commit message.

00:06:44.806 --> 00:06:51.506 align:middle
Clean slate, so run the recipe update command
again: Ooo, we have a bunch to update.

00:06:51.856 --> 00:06:53.236 align:middle
We'll go through them one-by-one.

00:06:53.686 --> 00:06:57.586 align:middle
Hitting enter will use the first one
in the list, doctrine/deprecations.

00:06:57.876 --> 00:07:01.906 align:middle
"No file changed...". Hmm, run
git status so see what's up.

00:07:02.586 --> 00:07:04.986 align:middle
Just the symfony.lock file was modified.

00:07:05.466 --> 00:07:09.786 align:middle
This is the internal config file Symfony
Flex uses to keep track of your recipes.

00:07:10.166 --> 00:07:17.616 align:middle
We can just commit this and move on: git
commit -a -m "update recipes" Onward!

00:07:18.176 --> 00:07:21.816 align:middle
Run the recipe update command
again: asset-mapper is next...

00:07:22.386 --> 00:07:24.066 align:middle
Just the symfony.lock file again.

00:07:24.876 --> 00:07:28.606 align:middle
When I update recipes, I like to keep
all the updates in a single commit.

00:07:28.766 --> 00:07:34.686 align:middle
So we'll amend the new changes with the
previous commit with: git commit -a --

00:07:34.896 --> 00:07:39.486 align:middle
amend This opens a text editor to
optionally update the commit message.

00:07:39.896 --> 00:07:41.406 align:middle
I'll just exit to keep it the same.

00:07:42.336 --> 00:07:44.366 align:middle
Now update the framework-bundle's recipe...

00:07:45.306 --> 00:07:47.606 align:middle
Ooo, this one has some real changes.

00:07:48.146 --> 00:07:52.836 align:middle
A cool thing about this command is it shows the
CHANGELOG from the symfony/recipe repository,

00:07:52.836 --> 00:07:56.246 align:middle
so you can easily dig in to
understand why a change was made.

00:07:57.566 --> 00:07:59.626 align:middle
Run git status to see the changes.

00:08:00.196 --> 00:08:04.886 align:middle
Aside from the symfony.lock file,
public/index.php was modified.

00:08:05.256 --> 00:08:06.076 align:middle
Let's check it out!

00:08:06.636 --> 00:08:10.026 align:middle
Open public/index.php and see the change...

00:08:10.606 --> 00:08:12.586 align:middle
Looks like the static keyword was added.

00:08:12.586 --> 00:08:16.666 align:middle
I think this is a helpful
micro-optimization, we'll keep it!

00:08:17.286 --> 00:08:21.086 align:middle
This is a good time to mention that you
should never blindly update recipes.

00:08:21.536 --> 00:08:26.226 align:middle
Always check the changes and if you're unsure
about a change, follow that changelog back

00:08:26.226 --> 00:08:29.366 align:middle
to the symfony/recipe repository
to find the reasoning.

00:08:30.026 --> 00:08:32.716 align:middle
Amend the commit and move
onto the next recipe update.

00:08:34.256 --> 00:08:36.686 align:middle
The monolog-bundle also has some changes.

00:08:37.806 --> 00:08:43.476 align:middle
Looks like package config, so open up
config/packages/monolog.yaml to see them.

00:08:46.186 --> 00:08:47.476 align:middle
Just some comments removed.

00:08:47.646 --> 00:08:48.356 align:middle
No big deal...

00:08:49.176 --> 00:08:49.976 align:middle
amend the commit.

00:08:51.106 --> 00:08:54.846 align:middle
Next, update the stimulus-bundle
recipe and see the changes.

00:08:55.756 --> 00:08:58.606 align:middle
Open assets/stimulus_bootstrap.js.

00:09:00.056 --> 00:09:02.506 align:middle
Just added some boilerplate stuff.

00:09:02.506 --> 00:09:06.946 align:middle
Amend the commit and move onto
updating the twig-bundle recipe.

00:09:09.316 --> 00:09:14.246 align:middle
Our base template was modified,
so open templates/base.html.twig.

00:09:16.696 --> 00:09:18.696 align:middle
Some FrankenPHP stuff was added.

00:09:19.066 --> 00:09:21.826 align:middle
We're not using FrankenPHP at
the moment but doesn't hurt

00:09:21.826 --> 00:09:23.646 align:middle
to leave it in case we do in the future!

00:09:24.536 --> 00:09:28.556 align:middle
Amend the commit and update the
last recipe: zenstruck/foundry.

00:09:31.486 --> 00:09:37.166 align:middle
The config file was modified, so open
config/packages/zenstruck_foundry.yaml.

00:09:38.176 --> 00:09:40.366 align:middle
Just a comment was updated, so no big deal.

00:09:40.996 --> 00:09:42.746 align:middle
Amend the commit and we should be done!

00:09:43.206 --> 00:09:45.666 align:middle
Run the recipe update command again to confirm.

00:09:46.146 --> 00:09:48.686 align:middle
Nice! "All packages appear to be up to date."

00:09:49.356 --> 00:09:51.746 align:middle
Before we check the app,
clear our cache again...

00:09:53.686 --> 00:09:54.996 align:middle
Now refresh the homepage.

00:09:56.466 --> 00:09:57.736 align:middle
2 deprecations.

00:09:58.696 --> 00:10:02.996 align:middle
The Doctrine autoloader one is still here, but
we'll ignore that until we upgrade Doctrine.

00:10:03.696 --> 00:10:07.576 align:middle
The Foundry one is still here too,
so the recipe update didn't fix it.

00:10:07.946 --> 00:10:09.206 align:middle
Let's fix it ourselves.

00:10:09.836 --> 00:10:13.626 align:middle
It's saying this
enable_auto_refresh_with_lazy_objects config is

00:10:13.626 --> 00:10:17.896 align:middle
going to be forced to true in
3.0, so let's set it to true now.

00:10:18.576 --> 00:10:23.566 align:middle
Copy the option and open
config/packages/zenstruck_foundry.yaml.

00:10:24.516 --> 00:10:29.416 align:middle
Under zenstruck_foundry, paste,
and set to true: Totally an aside,

00:10:29.576 --> 00:10:36.926 align:middle
but if you're curious what this weird &amp;dev and
*dev syntax is, this is a YAML anchor and alias.

00:10:37.726 --> 00:10:41.686 align:middle
The &amp;dev marks this config
as an anchor named dev,

00:10:42.166 --> 00:10:45.596 align:middle
and then the *dev is an alias
that references that anchor.

00:10:45.596 --> 00:10:49.956 align:middle
So this is saying, "for the test
environment, use the same config as dev".

00:10:50.606 --> 00:10:53.596 align:middle
It's a cool way to avoid
duplication in your YAML files.

00:10:54.556 --> 00:10:57.296 align:middle
Ok, back to the browser and
refresh the homepage.

00:10:58.036 --> 00:10:59.706 align:middle
There are no deprecations now!

00:11:00.816 --> 00:11:02.646 align:middle
But... let's clear the cache...

00:11:02.646 --> 00:11:03.646 align:middle
and refresh again...

00:11:07.486 --> 00:11:10.576 align:middle
3 now but these are the Doctrine
ones we'll fix later.

00:11:11.486 --> 00:11:15.236 align:middle
Ok, we've successfully upgraded
our app to PHP 8.4.

00:11:15.706 --> 00:11:19.956 align:middle
Next, we'll upgrade to Symfony
7.4, the last Symfony 7 version.

