Buy Access to Course
16.

Upgrading to Symfony 6.0

|

Share this awesome video!

|

Keep on Learning!

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Finally, it's time to upgrade to Symfony 6! Woo!

Rector Upgrades to 6.0

But first, just in case, let's run Rector one more time. Go back to Rector's repository, click the Symfony link, and... steal the same code that we had earlier. Paste that into our rector.php file. Then, just like we did for Symfony 5.4, change SymfonySetList to SymfonyLevelSetList, and this time, say UP_TO_SYMFONY_60.

28 lines | rector.php
// ... lines 1 - 14
return static function (ContainerConfigurator $containerConfigurator): void {
// ... lines 16 - 23
$containerConfigurator->import(SymfonyLevelSetList::UP_TO_SYMFONY_60);
$containerConfigurator->import(SymfonySetList::SYMFONY_CODE_QUALITY);
$containerConfigurator->import(SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION);
};

In theory, there shouldn't be any code differences needed between Symfony 5.4 and 6.0... though sometimes there are minor cleanups you can do once you have upgraded.

Let's run this and see what happens. Say:

vendor/bin/rector process src/

And... okay. It made one change. This is to our event subscriber: it added an array return type. This was done because, in the future, this interface may add an array return type. So now our code is future compatible.

// ... lines 1 - 9
class CheckVerifiedUserSubscriber implements EventSubscriberInterface
{
// ... lines 12 - 24
public static function getSubscribedEvents(): array
{
// ... lines 27 - 29
}
}

Upgrading via Composer

With that done, let's upgrade! In composer.json, we need to find the main Symfony libraries and change their version from 5.4.* to 6.0.*. Let's take the lazy way out and do that with a "Find & Replace".

111 lines | composer.json
{
// ... lines 2 - 5
"require": {
// ... lines 7 - 21
"symfony/asset": "6.0.*",
"symfony/console": "6.0.*",
"symfony/dotenv": "6.0.*",
// ... line 25
"symfony/form": "6.0.*",
"symfony/framework-bundle": "6.0.*",
// ... line 28
"symfony/property-access": "6.0.*",
"symfony/property-info": "6.0.*",
"symfony/proxy-manager-bridge": "6.0.*",
"symfony/routing": "6.0.*",
"symfony/runtime": "6.0.*",
"symfony/security-bundle": "6.0.*",
"symfony/serializer": "6.0.*",
"symfony/stopwatch": "6.0.*",
"symfony/twig-bundle": "6.0.*",
// ... line 38
"symfony/validator": "6.0.*",
// ... line 40
"symfony/yaml": "6.0.*",
// ... lines 42 - 45
},
"require-dev": {
// ... lines 48 - 50
"symfony/debug-bundle": "6.0.*",
// ... line 52
"symfony/var-dumper": "6.0.*",
"symfony/web-profiler-bundle": "6.0.*",
// ... line 55
},
// ... lines 57 - 103
"extra": {
"symfony": {
// ... line 106
"require": "6.0.*"
}
}
}

Awesome! Like before, we're not touching any Symfony libraries that are not part of the main package and which follow their own versioning scheme. Oh, and at the bottom, this did also change extra.symfony.require to 6.0.*.

So, we're ready! Just like before, we could say:

composer up 'symfony/*'

But... I'm not going to bother with that. Let's update everything with just:

composer up

And... it fails! Hmm. One of the libraries I'm using is babdev/pagerfanta-bundle... and apparently it requires PHP 7.2... but we're using PHP 8. If you look further, there are some errors about pagerfanta-bundle[v2.8.0] requiring symfony/config ^3.4 || ^4.4 || ^5.1, but not Symfony 6. So what's happening here? It turns out that pagerfanta-bundle[v2.8.0] does not support Symfony 6. Gasp!

Run

composer outdated

to see a list of outdated packages. Oooh! babdev/pagerfanta-bundle has a new version 3.6.1. Go into composer.json and find that... here it is! Change its version to ^3.6.

111 lines | composer.json
{
// ... lines 2 - 5
"require": {
// ... lines 7 - 9
"babdev/pagerfanta-bundle": "^3.6",
// ... lines 11 - 45
},
// ... lines 47 - 109
}

This is a major version upgrade. So it may contain some backwards compatibility breaks. We'll check into that in a minute. Try:

composer up

again and... it's doing it! Everything just upgraded to Symfony 6!

Fixing PasswordUpgraderInterface::upgradePassword()

And then... to celebrate... it immediately exploded while clearing the cache. Uh oh... I think we may have missed a deprecation:

In UserRepository, upgradePassword([...]): void must be compatible with PasswordUpgraderInterface.

If you want to see this in color, you can refresh the homepage to see the same thing.

By the way, in Symfony 5.4, we can now click this icon to copy the file path to our clipboard. Now, if I go back over to my editor, hit "shift" + "shift" and paste, I jump directly to the file - and even the line - where the problem is.

And... phew! PhpStorm is not happy. That's because the upgradePassword() method changed from requiring a UserInterface to requiring a PasswordAuthenticatedUserInterface. So we just need to change that and... done!

40 lines | src/Repository/UserRepository.php
// ... lines 1 - 8
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
// ... lines 10 - 18
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
// ... lines 21 - 28
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
// ... lines 31 - 37
}
}

Back at our terminal, if we run:

php bin/console cache:clear

Now it's happy. We're still getting some deprecations down here from a different library... but I'm going to ignore those. These come from a deprecated package that... I really just need to remove from this project entirely.

PagerFanta Updates

Let's go make sure the homepage works. It... doesn't!? We get

Attempted the load class QueryAdapter from namespace "Pagerfanta\Doctrine\ORM.

This shouldn't be a surprise... since we did upgrade pagerfanta-bundle from 2.8 to 3.6.

This is a situation where you need to find the GitHub page for the library and hope that they have an upgrade document. This one actually does. If you read this closely, you'd discover that a bunch of classes that were previously part of Pagerfanta have now been broken into independent libraries. So if we want to use this QueryAdapter, we need to install a separate package. Do that with:

composer require pagerfanta/doctrine-orm-adapter

Cool... and if we refresh now... another error? This one's even better:

Unknown function pagerfanta. Did you forget to run composer require pagerfanta/twig in question/homepage.html.twig?.

The Twig integration was also moved to its own package... so we need to run that command too:

composer require pagerfanta/twig

And... after that's done... it's alive! We have a Symfony 6 project! Woohoo! If we click around, things seem to be working just fine. We did it!

Checking for Outdated Packages

Over at our command line, run

composer outdated

to see all of the outdated packages we have left. The list is now very short. One package is knplabs/knp-markdown-bundle, which is fully upgraded... but it's been abandoned. If you have this in a real project, refactor it to use twig/markdown-extra. I'm not going to bother, but that's why it's on this list.

The biggest thing here is that doctrine/dbal has a new major version! So hey! While we're here upgrading things, let's upgrade it too! That's next, along with some final cleanups.