Symfony 5.1 弃用 RouteCollectionBuilder -> RoutingConfigurator

Symfony 5.1 Deprecation RouteCollectionBuilder -> RoutingConfigurator

我正在将 Symfony 项目从 5.0 更新到 5.1 有一个弃用提示说 RouteCollectionBuilder 已弃用,应该使用 RoutingConfigurator 代替。

确切消息是

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

这应该如何实施? 我应该更改 vendors 文件夹中的代码吗?

您需要更新 Kernel class 才能开始使用 RoutingConfigurator 而不是 RouteCollectionBuilder

您可以通过更新配方自动完成 (composer recipes:install symfony/framework-bundle --force)。

Symfony 在 GitHub 上有一个演示项目,非常适合检查此类更改,而无需创建新的本地项目。

只需查看 reworked Kernel.php 并更新全部内容,而不仅仅是 RoutingConfiguration 这将导致无法找到路由的其他错误。

如果有人在解决此折旧问题时遇到问题

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

这是我更新后的文件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 5.4 开始,根据 symfony/framework-bundle 配方,Kernel.php 可以像这样为空:

<?php

namespace App;

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

class Kernel extends BaseKernel
{
    use MicroKernelTrait;
}

来源食谱: https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/5.4/src/Kernel.php

出于某种原因,应用配方 (composer recipes:install symfony/framework-bundle --force) 没有更新内核文件。但是通过用上面的内容替换它,对我有用并修复了弃用警告。