laravel-cors
laravel-cors copied to clipboard
CORS doesn't works when I'm trying to `dd()`, `dump()`.
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
same if i remove dd its run perfectly.
did you find any solution ?
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.
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!
Thanks! Works like charm!