lumen和laravel中的跨域中间件

之前用laravel处理跨域的时候,预检请求(options)不需要认为处理;但是在lumen没有去处理反而报错

写了个跨域中间件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
namespace App\Http\Middleware;
use Closure;
class AllowCors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// header('Access-Control-Allow-Origin: localhost,vue.zhongtaihexin.net');
header("Access-Control-Allow-Origin: *");
// header("Access-Control-Allow-Credentials: false");
header("Access-Control-Allow-Methods: GET,POST,DELETE,PUT,OPTIONS");//允许的请求方式
// header("Access-Control-Allow-Methods: GET,POST,DELETE,PUT,OPTIONS");
header("Access-Control-Allow-Headers: Origin,Authorization,Accept,Content-Type");
header("Access-Control-Expose-Headers: *");//
header("Access-Control-Max-Age:599");//同一个url预检缓存设置
//lumen需要手动处理该类请求,laravel不用
if( strtolower($request->method()) === 'options')
return response()->json(null,200);
return $next($request);
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!