Last active
October 5, 2015 15:44
-
-
Save fabiocarneiro/81ea4e1eb0a52b8ded12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AddHeadersForEveryResponseMiddleware | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) | |
{ | |
return $response | |
->withAddedHeader('Access-Control-Allow-Origin', '*') | |
->withAddedHeader('Access-Control-Allow-Methods', '*') | |
->withAddedHeader('Access-Control-Allow-Headers', '*'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CORSHeadersInResponseMiddleware | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) | |
{ | |
return $next($request, new JsonResponse()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'routes' => [ | |
// no route for the current path | |
], | |
'middleware_pipeline' => [ | |
'pre_routing' => [ | |
[ | |
// deal with OPTIONS request only | |
'middleware' => AddHeadersOnlyForOptionsResponseMiddleware::class, | |
], | |
[ | |
'middleware' => AuthenticationMiddleware::class, | |
'path' => '/authentication', | |
], | |
], | |
'post_routing' => [ | |
[ | |
// add headers for CORS in other requests | |
'middleware' => AddHeadersForEveryResponseMiddleware::class, | |
], | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AddHeadersOnlyForOptionsResponseMiddleware | |
{ | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) | |
{ | |
if ('OPTIONS' !== $request->getMethod()) { | |
return $next($request, $response); | |
} | |
return $response | |
->withAddedHeader('Access-Control-Allow-Origin', '*') | |
->withAddedHeader('Access-Control-Allow-Methods', '*') | |
->withAddedHeader('Access-Control-Allow-Headers', '*'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment