开发环境中的 Symfony 错误:不存在的服务 "C"(链接到 webpack-dev-server)

Symfony error on dev environment: Non existant service "C" (linked to webpack-dev-server)

我用 webpack 开发服务器为开发环境制作了一个 Symfony 4 应用程序。

当我使用 bin/console cache:clear 命令或通过浏览器继续应用程序时,我收到一条错误消息,告诉我我请求了一个名为 "C".

的不存在的服务

我认为错误与资产的路由有关。 当我的开发服务器不是 运行 时启动错误,有时即使是。 但是,如果我删除 routing.yaml 中的 strict_requirements: true,则不会出现错误。

webpack.config.js

devServer: {
    public: 'dev.myapp.io:8080',
    contentBase: path.resolve(__dirname, 'public'),
    headers: {
        'Access-Control-Allow-Origin': '*'
    },
    watchOptions: {
        aggregateTimeout: 300,
        poll: 1000,
        ignored: /node_modules/
    },
    historyApiFallback: true,
    compress: true,
    noInfo: true,
    quiet: true,
    port: devServerPort,
}

config/packages/dev/framework.yaml

framework:
    router: { resource: '%kernel.project_dir%/config/packages/dev/routing.yaml' }
    profiler: { only_exceptions: false }
    assets:
        base_urls: 'http://localhost:8080'
        json_manifest_path: '%kernel.project_dir%/public/dist/manifest.json'

config/packages/dev/routing.yaml

framework:
    router:
        strict_requirements: true

错误

In Container.php line 277:
You have requested a non-existent service "C". Did you mean one of these: 
"MyApp\Controller\Home\HomeController", 
"Symfony\Bundle\FrameworkBundle\Controller\RedirectController", 
"Symfony\Bundle\FrameworkBundle\Controller\TemplateController"?

如果有人知道如何避免错误,我将不胜感激:) 我想保持 strict_requirements 为真,但我现在没有其他解决方案。

当配置跨文件复制时,会出现此错误。因此,例如,如果在您的 config/packages/framework.yaml 中您有

# config/packages/framework.yaml
framework:
    router:
      resource: "%kernel.root_dir%/config/routing.yml"
      strict_requirements: ~

然后你在

# config/packages/routing.yaml
framework:
    router:
#        strict_requirements: ~
        utf8: true

您可能会遇到您发布的错误。您不能在多个文件上复制配置。删除 routing.yaml 文件或从 config/packages/framework.yaml

中删除 router 密钥

希望对您有所帮助。