Symfony 5.1 Deprecation RouteCollectionBuilder -> RoutingConfigurator

Viewed 6065

I am updating a Symfony project from 5.0 to 5.1 There is this one deprecation hint saying RouteCollectionBuilder is deprecated and RoutingConfigurator should be used instead.

The exact message is

Since symfony/routing 5.1: The "Symfony\Component\Routing\RouteCollectionBuilder" class is deprecated, use "Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator" instead.

How is this supposed to be implemented? Am I supposed to change code in the vendors folder?

4 Answers

You need to update Kernel class to start using RoutingConfigurator instead of RouteCollectionBuilder.

You can do it automatically by updating the recipe (composer recipes:install symfony/framework-bundle --force).

If anyone has problems fixing this depreciation

Since symfony/routing 5.1: The "Symfony\Component\Routing\RouteCollectionBuilder" class is deprecated, use "Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator" instead.

Here is my updated file src/Kernel.php

<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    protected function configureContainer(ContainerConfigurator $container): void
    {
        $container->import('../config/{packages}/*.yaml');
        $container->import('../config/{packages}/'.$this->environment.'/*.yaml');

        if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
            $container->import('../config/services.yaml');
            $container->import('../config/{services}_'.$this->environment.'.yaml');
        } elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
            (require $path)($container->withPath($path), $this);
        }
    }

    protected function configureRoutes(RoutingConfigurator $routes): void
    {
        $routes->import('../config/{routes}/'.$this->environment.'/*.yaml');
        $routes->import('../config/{routes}/*.yaml');

        if (is_file(\dirname(__DIR__).'/config/routes.yaml')) {
            $routes->import('../config/routes.yaml');
        } elseif (is_file($path = \dirname(__DIR__).'/config/routes.php')) {
            (require $path)($routes->withPath($path), $this);
        }
    }
}

Symfony has a demo project on GitHub which is perfect to checkout changes like that without having to create a new local project.

Just have a look at the reworked Kernel.php and update the entire content, not just the RoutingConfiguration which will lead to other errors that routes can't be found.

From Symfony 5.4 onwards, accordingly to the symfony/framework-bundle recipe, Kernel.php can be empty like this:

<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;
}

Source recipe: https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/5.4/src/Kernel.php

For some reason, applying the recipe (composer recipes:install symfony/framework-bundle --force) didn't updated the kernel file. But by replacing it with the above content, worked for me and fixed the deprecation warning.

Related