laravel-cors icon indicating copy to clipboard operation
laravel-cors copied to clipboard

CORS doesn't works when I'm trying to `dd()`, `dump()`.

Open fesh-biz opened this issue 3 years ago • 4 comments

All requests works fine until I'm not trying to dd(), dump()

I had read several treads and didn't found solution for me, so maybe this will helps.

This is doc quote from here https://packagist.org/packages/barryvdh/laravel-cors

If you echo(), dd(), die(), exit(), dump() etc in your code, you will break the Middleware flow. When output is sent before headers, CORS cannot be added. When the scripts exits before the CORS middleware finished, CORS headers will not be added. Always return a proper response or throw an Exception.

To solve it In my case I had simply added 2 helpers and added it in composer.json

composer.json

...
"autoload": {
        "files": [
            "app/Helpers/helpers.php"
        ],
...

app/Helpers/helpers.php

/*
 * dd() with headers
 */
if (!function_exists('ddh')) {
    function ddh($var){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: *');
        header('Access-Control-Allow-Headers: *');
        dd($var);
    }
}

/*
 * dump() with headers
 */
if (!function_exists('dumph')) {
    function dumph($var){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: *');
        header('Access-Control-Allow-Headers: *');
        dump($var);
    }
}

P.S. I don't know how to use issue form correctly so I hope that I didn't broke any rules. Just wanted to help

fesh-biz avatar Mar 23 '21 13:03 fesh-biz

same if i remove dd its run perfectly.

did you find any solution ?

ahmedgadit avatar May 22 '21 07:05 ahmedgadit

same if i remove dd its run perfectly.

did you find any solution ?

There is solution in my message above. You need to add 2 functions ddh, dumph. ddh means that this is still dd but now it has necessary headers inside.

fesh-biz avatar Jun 01 '21 13:06 fesh-biz

All requests works fine until I'm not trying to dd(), dump()

I had read several treads and didn't found solution for me, so maybe this will helps.

This is doc quote from here https://packagist.org/packages/barryvdh/laravel-cors

If you echo(), dd(), die(), exit(), dump() etc in your code, you will break the Middleware flow. When output is sent before headers, CORS cannot be added. When the scripts exits before the CORS middleware finished, CORS headers will not be added. Always return a proper response or throw an Exception.

To solve it In my case I had simply added 2 helpers and added it in composer.json

composer.json

...
"autoload": {
        "files": [
            "app/Helpers/helpers.php"
        ],
...

app/Helpers/helpers.php

/*
 * dd() with headers
 */
if (!function_exists('ddh')) {
    function ddh($var){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: *');
        header('Access-Control-Allow-Headers: *');
        dd($var);
    }
}

/*
 * dump() with headers
 */
if (!function_exists('dumph')) {
    function dumph($var){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: *');
        header('Access-Control-Allow-Headers: *');
        dump($var);
    }
}

P.S. I don't know how to use issue form correctly so I hope that I didn't broke any rules. Just wanted to help

works very well, thx!

williamjkc69 avatar Aug 20 '21 03:08 williamjkc69

Thanks! Works like charm!

julesbloemen avatar Aug 24 '21 17:08 julesbloemen