laravel ,解决跨域问题

http://laravelacademy.org/post/9273.html

关于Larave扩展laravel-cors使用的

laravel-cors的作用是用于解决浏览器跨域的问题

  1. 安装

在终端执行安装命令如下:

composer require barryvdh/laravel-cors

  1. 添加服务提供商

在Laravel配置文件app.phpproviders数组中添加如下配置:

Barryvdh\Cors\ServiceProvider::class,

  1. 发布配置文件

执行在终端执行发布配置文件命令如下:

php artisan vendor:publish --provider="Barryvdh\\Cors\\ServiceProvider"

执行后会在laravel目录下的config目录中新增cors.php配置文件,如下图

至此laravel-Cors安装完成。

详细信息:https://github.com/barryvdh/laravel-cors

Global usage

To allow CORS for all your routes, add the HandleCors middleware in the $middleware property of app/Http/Kernel.phpclass:

protected $middleware = [    // ...    \Barryvdh\Cors\HandleCors::class,];

Group middleware

If you want to allow CORS on a specific middleware group or route, add the HandleCors middleware to your group:

protected $middlewareGroups = [    'web' => [       // ...    ],                                         'api' => [        // ...        \Barryvdh\Cors\HandleCors::class,                                          ],                                 ];

王加文博客
请先登录后发表评论
  • latest comments
  • 总共0条评论