Symfony2 dev服务器和nelmio cors捆绑

时间:2015-03-16 03:56:32

标签: php symfony cors devserver

我正在使用symfony开发一个API,当然我已经考虑了cors。我安装了nelmio-cors-bundle但是标头没有到达客户端:

我的配置是:

nelmio_cors:
    defaults:
        allow_credentials: true
        allow_origin: []
        allow_headers: []
        allow_methods: []
        expose_headers: []
        max_age: 0
        hosts: []
        origin_regex: false
    paths:
        '^/api':
            allow_origin: ['*']
            allow_headers: ['*']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600

我找回了以下标题:

Allow → GET
Cache-Control → no-cache
Connection → close
Content-Type → application/json
Date → Mon, 16 Mar 2015 03:46:41 GMT
Host → 127.0.0.1:8000
X-Debug-Token → a6ec46
X-Debug-Token-Link → /_profiler/a6ec46
X-Powered-By → PHP/5.5.21

CORS标题不存在。

你能帮帮我吗?我在symfony的开发服务器上运行我的应用程序,这是一个问题吗?

感谢。

1 个答案:

答案 0 :(得分:0)

正如官方文件中提到的那样:

  

allow_origin和allow_headers可以设置为*来接受任何值,   允许的方法但必须明确列出。路径必须   至少包含一个项目。

这是一个sapmle配置:

nelmio_cors:
  defaults:
    allow_credentials: false
    allow_origin: ['^http://localhost:[0-9]+']
    allow_headers: ['*']
    allow_methods: ['POST', 'GET']
    expose_headers: ['*']
    max_age: 0
    hosts: []
    origin_regex: false
    forced_allow_origin_value: ~
  paths:
    '^/':
        origin_regex: true
        allow_origin: ['^http://localhost:[0-9]+']
        allow_headers: ['Authorization']
        allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
        max_age: 3600