Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 9 |
Cors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 9 |
handle | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 9 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\MicroService\Middleware; |
4 | |
5 | use Closure; |
6 | use Illuminate\Support\Facades\Log; |
7 | |
8 | class Cors |
9 | { |
10 | /** |
11 | * Handle an incoming request. |
12 | * |
13 | * @param \Illuminate\Http\Request $request |
14 | * @param \Closure $next |
15 | * @return mixed |
16 | */ |
17 | |
18 | protected $headers = [ |
19 | 'Access-Control-Allow-Origin' => '*', |
20 | 'Access-Control-Allow-Methods' => 'GET,PUT,POST,DELETE,PATCH,OPTIONS', |
21 | 'Access-Control-Allow-Headers' => 'Authorization, X-Requested-With, Origin, X-Csrftoken, Content-Type, Accept, x-auth-type', |
22 | 'Access-Control-Expose-Headers' => 'Authorization, Expiration-time, x-auth-type, Content-disposition' |
23 | ]; |
24 | |
25 | |
26 | public function handle($request, Closure $next) |
27 | { |
28 | $handle = $next($request); |
29 | |
30 | if ($request->getMethod() == "OPTIONS") { |
31 | $response = response([], 200, $this->headers); |
32 | |
33 | return $response; |
34 | } |
35 | |
36 | if (method_exists($handle, 'header')) { |
37 | $handle->withHeaders($this->headers); |
38 | } else { |
39 | foreach ($this->headers as $key => $value) { |
40 | $handle->headers->set($key, $value); |
41 | } |
42 | } |
43 | |
44 | return $handle; |
45 | } |
46 | } |