This repository was archived by the owner on Feb 16, 2023. It is now read-only.
File tree 2 files changed +22
-6
lines changed
2 files changed +22
-6
lines changed Original file line number Diff line number Diff line change 6
6
use Illuminate \Foundation \Application as LaravelApplication ;
7
7
use Illuminate \Support \ServiceProvider as BaseServiceProvider ;
8
8
use Laravel \Lumen \Application as LumenApplication ;
9
+ use Illuminate \Foundation \Http \Events \RequestHandled ;
9
10
10
11
class CorsServiceProvider extends BaseServiceProvider
11
12
{
@@ -34,6 +35,13 @@ public function boot()
34
35
} elseif ($ this ->app instanceof LumenApplication) {
35
36
$ this ->app ->configure ('cors ' );
36
37
}
38
+
39
+ // Add the headers on the Request Handled event as fallback in case of exceptions
40
+ if (class_exists (RequestHandled::class) && $ this ->app ->bound ('events ' )) {
41
+ $ this ->app ->make ('events ' )->listen (RequestHandled::class, function (RequestHandled $ event ) {
42
+ $ this ->app ->make (HandleCors::class)->onRequestHandled ($ event );
43
+ });
44
+ }
37
45
}
38
46
39
47
/**
Original file line number Diff line number Diff line change 4
4
5
5
use Closure ;
6
6
use Asm89 \Stack \CorsService ;
7
+ use Illuminate \Contracts \Http \Kernel ;
7
8
use Illuminate \Foundation \Http \Events \RequestHandled ;
8
9
use Illuminate \Http \Request ;
9
10
use Illuminate \Contracts \Container \Container ;
@@ -46,12 +47,6 @@ public function handle($request, Closure $next)
46
47
return $ response ;
47
48
}
48
49
49
- // Add the headers on the Request Handled event as fallback in case of exceptions
50
- if (class_exists (RequestHandled::class) && $ this ->container ->bound ('events ' )) {
51
- $ this ->container ->make ('events ' )->listen (RequestHandled::class, function (RequestHandled $ event ) {
52
- $ this ->addHeaders ($ event ->request , $ event ->response );
53
- });
54
- }
55
50
56
51
// Handle the request
57
52
$ response = $ next ($ request );
@@ -80,6 +75,19 @@ protected function addHeaders(Request $request, Response $response): Response
80
75
return $ response ;
81
76
}
82
77
78
+ /**
79
+ * Add the headers to the Response, if they don't exist yet.
80
+ *
81
+ * @param RequestHandled $event
82
+ */
83
+ public function onRequestHandled (RequestHandled $ event )
84
+ {
85
+ if ($ this ->shouldRun ($ event ->request ) && $ this ->container ->make (Kernel::class)->hasMiddleware (static ::class)) {
86
+ $ this ->addHeaders ($ event ->request , $ event ->response );
87
+ }
88
+ }
89
+
90
+
83
91
/**
84
92
* Determine if the request has a URI that should pass through the CORS flow.
85
93
*
You can’t perform that action at this time.
0 commit comments