Created
September 7, 2017 18:53
-
-
Save dmitrymomot/36f00db2a67fe2da526ffa9093602d78 to your computer and use it in GitHub Desktop.
Headers for json api based on Laravel 5.5
This file contains hidden or 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 | |
namespace App\Http\Middleware; | |
use Closure; | |
class ApiHeaders | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$response = $next($request); | |
// CORS | |
if (env('CORS_ENABLE', false)) { | |
$origin = array_key_exists('HTTP_ORIGIN', $_SERVER) ? $_SERVER['HTTP_ORIGIN'] : '*'; | |
$credentials = ($origin == '*') ? 'false' : 'true'; | |
$response->headers->set('Access-Control-Allow-Origin' , $origin); | |
$response->headers->set('Access-Control-Allow-Credentials', $credentials); | |
$response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE'); | |
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With'); | |
} | |
// API content type | |
$content_type = env('API_CONTENT_TYPE', false); | |
if ($content_type) | |
$response->headers->set('Content-Type', $content_type); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment