0
点赞
收藏
分享

微信扫一扫

laravel解决跨域

自由情感小屋 2022-11-20 阅读 168


最新配置与以前不一样了,参考github:

1、composer require barryvdh/laravel-cors
//github搜搜 barryvdh/laravel-cors

2、在config/app.php中的provider中配置:
Barryvdh\Cors\ServiceProvider::class

3、配置跨域
3.1 配置一个api组都允许跨域
在/app/Http/Kernel.php中的$middlewareGroups的api中
添加:\Barryvdh\Cors\HandleCors::class,这样整个api组
都允许跨域
路由:
Route::get('/xxx', function () {
return 'xxx';
}); //默认所有api下都已经配置了

3.2 配置单个路由
在/app/Http/Kernel.php中的$$routeMiddleware中添加
'cors'=>\Barryvdh\Cors\HandleCors::class
路由:
Route::middleware('cors')->get
('/xxx', function () {
return 'xxx';
}); //单个路由配置跨域

4、生成跨域配置文件config/cors.php:
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"


举报

相关推荐

0 条评论