0

From my React Frontend I send a request to my Symfony backend.(Previously it worked fine with a pure PHP backend) .

useEffect(() => {
              axios.get('http://localhost:8080/real_estate_refactored_backend/main').then((response) => {
                console.log(response);
            })
            .catch((error) => {
                console.log(error.message);
            });
  });

The main in the URL is the route of my Symfony controller

<?php

namespace App\Controller;

use App\Entity\Estates;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MainController extends AbstractController
{
    /**
     * @Route("/main", name="main")
     */
    public function index(): Response
    {
        $entityManager = $this->getDoctrine()->getManager();
        $estates = $entityManager->getRepository(Estates::class)->findAll();

        $response = new Response();
   

    $response->setContent(json_encode([
        'estates' => uniqid(),
    ]));

    $response->headers->set('Content-Type', 'application/json');
  
    $response->headers->set('Access-Control-Allow-Origin', '*');
    return new Response($response);
    }
}

In Symfony the connection with the database went fine.

And eventhough I have installed the Nelmino CORS bundle in Symfony with the following configuration in the

nelmino_cors.yaml

nelmio_cors:
    defaults:
        origin_regex: true
        allow_origin: ['*']
        allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
        allow_headers: ['*']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': null

I still get the following error message

enter image description here

What can I do?

4
  • 1
    Hello, could you try to change the paths (in nelmio_cors.yaml) to: '^/': ~ ? Commented Mar 23, 2021 at 15:31
  • And erasing tje other stuff? Yes I did with the same result. Commented Mar 23, 2021 at 15:45
  • 1
    Did you tried to clear your cache? As mentionned here: stackoverflow.com/a/54572925/1750964 Commented Mar 23, 2021 at 16:11
  • Yes I tried but it doesn`t change a thing. Commented Mar 23, 2021 at 16:19

2 Answers 2

1

You need to add this code to .env

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^.*$
###< nelmio/cors-bundle ###
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have Symfony (security) configured to use a session cookie for authention, add allow_credentials: true to nelmio_cors.yaml, and add withCredentials: true to the axios configuration object:

config/packages/nelmio_cors.yaml

nelmio_cors:
    defaults:
        allow_credentials: true
        # Your existing configuration...

Your axios snippet

axios.get('/foo', { withCredentials: true });

2 Comments

Can you trell me where I have to go to do this config with cookies?
What I meant is that I assume you have symfony's security.yaml configured to use a firewall based on session/cookie authentication. You don't need to do anything with that configuration if that's the case. Just apply the examples I provided in nelmio_cors.yaml and your axios instance configuration (while keeping everything the same as in your original post). This allows your frontend and backend to share sessions/cookies despite being hosted on separate origins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.