Last active
May 26, 2016 13:26
-
-
Save purwandi/5bb0a1f7ac26cbc5396c93cb90473935 to your computer and use it in GitHub Desktop.
Laravel CORS
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 ApiMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
return $next($request) | |
->header('Access-Control-Allow-Origin', '*') | |
->header('Access-Control-Allow-Credentials', 'true') | |
->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE') | |
->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With') | |
->header('Access-Control-Max-Age', '3600'); | |
} | |
} |
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; | |
use Illuminate\Foundation\Http\Kernel as HttpKernel; | |
class Kernel extends HttpKernel | |
{ | |
/** | |
* The application's global HTTP middleware stack. | |
* | |
* These middleware are run during every request to your application. | |
* | |
* @var array | |
*/ | |
protected $middleware = [ | |
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, | |
]; | |
/** | |
* The application's route middleware groups. | |
* | |
* @var array | |
*/ | |
protected $middlewareGroups = [ | |
'web' => [ | |
\App\Http\Middleware\EncryptCookies::class, | |
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, | |
\Illuminate\Session\Middleware\StartSession::class, | |
\Illuminate\View\Middleware\ShareErrorsFromSession::class, | |
\App\Http\Middleware\VerifyCsrfToken::class, | |
], | |
'api' => [ | |
'throttle:60,1', | |
\App\Http\Middleware\ApiMiddleware::class // <-- Add ApiMiddleware class in here | |
], | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment