fruitcake / laravel-cors

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
MIT License
6.27k stars 614 forks source link

CORS not working with ->send() #467

Closed zZeepo closed 4 years ago

zZeepo commented 4 years ago

Hey, I'm currently facing a problem in my Lumen application. Everything works fine, but whenever I want to create a response like this response()->json([], 200)->send(); instead of returning it (return response()->json([], 200);), I get a CORS error in my frontend.

If I'm not mistaken, the problem is that the HandleCors class execution is stopped in line 57 and the headers are therefore never added. https://github.com/fruitcake/laravel-cors/blob/master/src/HandleCors.php#L57

But I was wondering if there is a workaround to make CORS work in combiniation with the ->send() method. What do you guys think about this?

These are my lumen & cors versions: fruitcake/laravel-cors: v2.0.1 laravel/lumen-framework: v6.3.5

barryvdh commented 4 years ago

No, why would you send the response? That breaks the middleware flow.

zZeepo commented 4 years ago

I was using the send to create responses in nested function calls without the need to propagate the response to the initial function call. Alternatively I could use custom exceptions for this, but I kind of liked the ->send() method, because it seemed more dynamic for this purpose.

barryvdh commented 4 years ago

Yes unfortunately it breaks the middleware as it sends headers directly. I suggest throwing am HttpException instead.

zZeepo commented 4 years ago

Then I'll go that route for now. Thanks for the response. If you want you can mark this issue as closed.