16

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?

user3440145
  • 793
  • 10
  • 34

4 Answers4

27

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).

Leprechaun
  • 759
  • 5
  • 14
  • 1
    Just be aware that the 5.1 Kernel.php was also changed to only load yaml config file. If you happen to use php or xml config files then you need to make a few additional tweaks. You also risk overwriting any changes you may have already made to Kernel.php. An alternative approach is to create a fresh 5.1 project and then do a bit of diff/copy/pasting. – Cerad Jun 04 '20 at 15:58
  • 1
    Thanks a lot for your answere. I did not change Kernel.php so far so that's OK and also I strictly used yaml for configuration. However the recipe update changed some other files too which I have to review. Looks very promising though :-) – user3440145 Jun 04 '20 at 16:06
  • ....and to provide another hint: the project dir method has vanished from the original kernel. That has caused problems for me – Nico Haase Jun 05 '20 at 06:33
  • 2
    took me 5 minutes to figure out that RoutingConfigurator::import() method has a different signature than previous RouteCollectionBuilder::import() method. $prefix param is gone ;-) – Axi Sep 10 '21 at 12:37
13

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);
        }
    }
}
arno
  • 792
  • 14
  • 33
2

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.

Manuel
  • 414
  • 5
  • 11
2

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.

Ricardo Martins
  • 5,702
  • 3
  • 40
  • 59