Symfony as API: CORS problem – Stopped propagation

  Kiến thức lập trình

I am currently working on a small project, where a little issue suddenly started arising. Requests from a Frontend, which is also local due to development purposes, started having CORS errors. The funny thing about is, that normal requests through for example Postman do succeed.

A Cors error can also mean something else is wrong. However, I can’t find evidence in my terminal for this. I only have one big exception, and I can’t really put my finger on it how to handle with it. I’ve updated, I’ve looked through all Cors errors that relate that I can find here, and even tested an older project, which doesn’t have the problem occuring. Then I thought it was a permission problem, since it couldn’t write into cache and log folders, but since this has been fixed, there is still no communication possible between my React frontend and Symfony with API Platform backend.

The error in the Terminal is as follows;

nginx stdout | 2024/04/13 14:14:11 [error] 25#25: *410 FastCGI sent in stderr: "PHP message: [debug] Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerDebugHandlersListener::configure".; PHP message: [debug] Notified event "kernel.request" to listener "AppEventListenerRequestEventListener::onKernelRequest".; PHP message: [debug] Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerValidateRequestListener::onKernelRequest".; PHP message: [debug] Notified event "kernel.request" to listener "NelmioCorsBundleEventListenerCorsListener::onKernelRequest".; PHP message: [debug] Listener "NelmioCorsBundleEventListenerCorsListener::onKernelRequest" stopped propagation of the event "kernel.request".; PHP message: [debug] Listener "SymfonyComponentHttpKernelEventListenerSessionListener::onKernelRequest" was not called for event "kernel.request".; PHP message: [debug] Listener "SymfonyComponentHttpKernelEventListenerLocaleListener::setDefaultLocale" was not called for event "kernel.request".; PHP message: [debug] Listener "SymfonyComponentHttpKernelEventListenerRouterListener::onKernelRequest" was not called for event "kernel.request".; PHP message: [debug] Listener "ApiPlatformSymfonyEventListenerAddFormatListener::onKernelRequest" was not called for event "kernel.request".; PHP message: [debug] Listener "ApiPlatformSymfonyEventListenerQueryParameterValidateListener::onKernelRequest" was not called for event "kernel.request".; PHP message: [debug] Listener "SymfonyComponentHttpKernelEventListenerLocaleListener::onKernelRequest" was not called for event "kernel.request".; PHP message: [debug] Listener "SymfonyComponentHttpKernelEventListenerLocaleAwareListener::onKernelRequest" was not called for event "kernel.request".; PHP message: [debug] Listener "SymfonyBundleSecurityBundleDebugTraceableFirewallListener::configureLogoutUrlGenerator" was not called for event "kernel.request".;

But as I said, this only happens when I call from the local site, testsite.local:5173, where the API lays in testsite.local. A request through a Request GUI or through the console is working. So, since the error appears to only happen with the Frontend, I thought maybe something is wrong there. However, the GUI sends the same headers, and no issues appear. Then I thought maybe my CORS configuration is wrong. However, even here I just don’t see any relevant problem.

config/packages/nelmio_cors.yaml

nelmio_cors:
    defaults:
        origin_regex: true
        allow_credentials: true
        allow_origin: ["%env(CORS_ALLOW_ORIGIN)%"]
        allow_methods: ["GET", "OPTIONS", "POST", "PUT", "PATCH", "DELETE"]
        allow_headers: ["*"]
        expose_headers: ["Link", "Set-Cookie"]
        max_age: 3600

And the CORS_ALLOW_ORIGIN variable is

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN='^https?://(localhost|127.0.0.1|testsite.local)(:[0-9]+)?(/)?$'
###< nelmio/cors-bundle ###

Maybe I am missing something, or maybe I am overlooking something. If I boot up other projects with the same backend and frontend there are no issues, but with this one I really do not have a clue why I can’t send any requests from the frontend to the backend, without a CORS error and the request being denied. It does also not popup in the profiler.

My best bet is that some events are not called, but I can’t place why. And I do not know what is wrong with the CorsListener. That’s where I think the problem is, but any research online on it didn’t help me any further.

Best regards,

LEAVE A COMMENT