定义 "nelmio_cors" 不支持配置键 "defaults"

The configuration key "defaults" is unsupported for definition "nelmio_cors"

我正在尝试为我的 api 启用 CORS(我使用的是 Symfony 4.2 + api_platform)

我已经安装了 NelmiosCors

composer req "nelmio/cors-bundle"

添加到 Kernel.php

$bundles = array(
    new Nelmio\CorsBundle\NelmioCorsBundle(),
);

并将此添加到 services.yaml

nelmio_cors:
    defaults:
        allow_credentials: false
        allow_origin: []
        allow_headers: ['Content-Type']
        allow_methods: []
        expose_headers: []
        max_age: 0
        hosts: []
        origin_regex: false
        forced_allow_origin_value: ~
    paths:
        '^/api/':
            allow_origin: ['*']
            allow_headers: ['X-Custom-Auth', 'Content-Type']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600
        '^/':
            origin_regex: true
            allow_origin: ['^http://localhost:[0-9]+']
            allow_headers: ['X-Custom-Auth', 'Content-Type']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600
            hosts: ['^api\.']

但是当我这样做时 server:run 我得到一个错误:

In FileLoader.php line 166:
!!
!!    The configuration key "defaults" is unsupported for definition "nelmio_cors
!!    " in "/mnt/c/dev/api/config/services.yaml". Allowed configuration keys are
!!    "alias", "parent", "class", "shared", "synthetic", "lazy", "public", "abstr
!!    act", "deprecated", "factory", "file", "arguments", "properties", "configur
!!    ator", "calls", "tags", "decorates", "decoration_inner_name", "decoration_p
!!    riority", "autowire", "autoconfigure", "bind" in /mnt/c/dev/api/config/serv
!!    ices.yaml (which is loaded in resource "/mnt/c/dev/api/config/services.yaml
!!    ").
!!
!!
!!  In YamlFileLoader.php line 813:
!!
!!    The configuration key "defaults" is unsupported for definition "nelmio_cors
!!    " in "/mnt/c/dev/api/config/services.yaml". Allowed configuration keys are
!!    "alias", "parent", "class", "shared", "synthetic", "lazy", "public", "abstr
!!    act", "deprecated", "factory", "file", "arguments", "properties", "configur
!!    ator", "calls", "tags", "decorates", "decoration_inner_name", "decoration_p
!!    riority", "autowire", "autoconfigure", "bind".

我遵循了 NelmiosCors 指南 + api_platform 指南,现在我不知道我能做什么。

一些帮助将不胜感激。

在 Symfony 4.x 中,您必须为所有包使用单独的配置,但不能在 service.yaml

中使用

config/package/[=17= 中创建一个名为 nelmio_cors.yaml 的文件] 并把你所有的 nelmio_cors 配置!