Core: [Question] CORS

Created on 25 Jun 2017  路  2Comments  路  Source: api-platform/core

How can I add Access-Control-Allow-Origin to API? The following sentense is stated in docs

_If you are starting a new project, the easiest way to get API Platform up is to install the API Platform Standard Edition. It ships with the API Platform Core library integrated with the Symfony framework, the schema generator, Doctrine ORM, NelmioApiDocBundle, NelmioCorsBundle and Behat. Basically, it is a Symfony edition packaged with the best tools to develop a REST API and sensible default settings._

But inside of composer.json

    "require": {
        "php": ">=7.0",

        "doctrine/inflector": "^1.0",
        "psr/cache": "^1.0",
        "psr/container": "^1.0",
        "symfony/http-foundation": "^3.1 || ^4.0",
        "symfony/http-kernel": "^2.7 || ^3.0 || ^4.0",
        "symfony/property-access": "^2.7 || ^3.0 || ^4.0",
        "symfony/property-info": "^3.1 || ^4.0",
        "symfony/serializer": "^3.1 || ^4.0",
        "willdurand/negotiation": "^2.0.3"
    },
    "require-dev": {
        "behat/behat": "^3.1",
        "behat/mink": "^1.7",
        "behat/mink-browserkit-driver": "^1.3.1",
        "behat/mink-extension": "^2.2",
        "behat/symfony2-extension": "^2.1.1",
        "behatch/contexts": "^2.6",
        "doctrine/doctrine-bundle": "^1.6.3",
        "doctrine/orm": "^2.5.2",
        "doctrine/annotations": "^1.2",
        "friendsofsymfony/user-bundle": "^2.0",
        "guzzlehttp/guzzle": "^6.0",
        "nelmio/api-doc-bundle": "^2.11.2",
        "php-mock/php-mock-phpunit": "^1.1",
        "phpdocumentor/reflection-docblock": "^3.0",
        "phpdocumentor/type-resolver": "^0.2",
        "phpunit/phpunit": "^5.6.8",
        "psr/log": "^1.0",
        "sensio/framework-extra-bundle": "^3.0.11 || ^4.0",
        "symfony/asset": "^3.3 || ^4.0",
        "symfony/cache": "^3.3 || ^4.0",
        "symfony/config": "^3.3 || ^4.0",
        "symfony/console": "^3.3 || ^4.0",
        "symfony/debug": "^2.8 || ^3.0 || ^4.0",
        "symfony/dependency-injection": "^3.3 || ^4.0",
        "symfony/doctrine-bridge": "^2.8 || ^3.0 || ^4.0",
        "symfony/event-dispatcher": "^3.3 || ^4.0",
        "symfony/expression-language": "^2.8 || ^3.0 || ^4.0",
        "symfony/finder": "^3.3 || ^4.0",
        "symfony/form": "^3.3 || ^4.0",
        "symfony/framework-bundle": "^3.3 || ^4.0",
        "symfony/phpunit-bridge": "^2.7 || ^3.0 || ^4.0",
        "symfony/routing": "^3.3 || ^4.0",
        "symfony/security": "^3.0 || ^4.0",
        "symfony/security-bundle": "^3.0 || ^4.0",
        "symfony/twig-bundle": "^3.1 || ^4.0",
        "symfony/validator": "^3.3 || ^4.0",
        "symfony/yaml": "^3.3 || ^4.0"
    },

There's no NelmioCORSBundle at all..., I've tried installing bundle but it has no effect

question

Most helpful comment

Hello, I have the same kind of problem

I had to turn the parameters pagination_enabled to false in one of my entity in api-platform. Unfortunately when I do so I have an error in the client part :

1- [Error] Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

2- [Error] Failed to load resource: Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin. (codeable_concepts, line 0)

3- [Error] Fetch API cannot load http://localhost:8080/codeable_concepts. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

I've installed nelmio/cors-bundle and the parameters are

capture d ecran 2018-10-03 a 11 46 41

How can I fix it ?

Thanks

All 2 comments

You have to add that bundle into the composer.json of the app :

 "nelmio/cors-bundle": "^1.4",

and then you have to add this to the config.yml

` nelmio_cors: defaults: allow_origin: ['%cors_allow_origin%'] allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'] allow_headers: ['content-type', 'authorization'] expose_headers: ['link'] max_age: 3600 paths: '^/': ~

and use defined that parameter : cors_allow_origin in parameters.yml

Hello, I have the same kind of problem

I had to turn the parameters pagination_enabled to false in one of my entity in api-platform. Unfortunately when I do so I have an error in the client part :

1- [Error] Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

2- [Error] Failed to load resource: Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin. (codeable_concepts, line 0)

3- [Error] Fetch API cannot load http://localhost:8080/codeable_concepts. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

I've installed nelmio/cors-bundle and the parameters are

capture d ecran 2018-10-03 a 11 46 41

How can I fix it ?

Thanks

Was this page helpful?
0 / 5 - 0 ratings