Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

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

Open
fesh-biz opened this issue Mar 23, 2021 · 4 comments
Open

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

fesh-biz opened this issue Mar 23, 2021 · 4 comments

Comments

@fesh-biz
Copy link

fesh-biz commented Mar 23, 2021

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

@ahmedgadit
Copy link

same if i remove dd its run perfectly.

did you find any solution ?

@fesh-biz
Copy link
Author

fesh-biz commented Jun 1, 2021

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.

@williamjkc69
Copy link

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!

@julesbloemen
Copy link

Thanks! Works like charm!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants