Add middleware to set Content-type to application/json on all API requests

This commit is contained in:
Nabeel Shahzad 2017-12-28 22:50:07 -06:00
parent d04c11f660
commit a5c5518a12
2 changed files with 20 additions and 0 deletions

View File

@ -37,6 +37,7 @@ class Kernel extends HttpKernel
'api' => [
'throttle:60,1',
'bindings',
'json',
],
];
@ -54,6 +55,7 @@ class Kernel extends HttpKernel
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'json' => \App\Http\Middleware\JsonResponse::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
#'role' => \Laratrust\Middleware\LaratrustRole::class,
#'permission' => \Laratrust\Middleware\LaratrustPermission::class,

View File

@ -0,0 +1,18 @@
<?php
/**
* Set the content type in the API layer
*/
namespace App\Http\Middleware;
use Closure;
class JsonResponse
{
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}