定义“ nelmio_cors”不支持配置键“ defaults”

时间:2019-01-20 16:30:17

标签: symfony cors api-platform.com

我正在尝试为我的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指南,现在我不知道该怎么办。

一些帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

在Symfony 4.x中,必须对所有捆绑包使用单独的配置,但不能在service.yaml内部使用

创建一个名为 nelmio_cors.yaml 的文件,并创建 config / package / 并放置所有nelmio_cors配置!