Skip to content

Instantly share code, notes, and snippets.

@waptik
Forked from meSingh/JsonMiddleware.php
Created July 23, 2018 01:24
Show Gist options
  • Save waptik/c24067536ca3a1a18f72a78d0bb10276 to your computer and use it in GitHub Desktop.
Save waptik/c24067536ca3a1a18f72a78d0bb10276 to your computer and use it in GitHub Desktop.
If you are using laravel as api only, you might want to return JSON on every request, even on errors/exceptions. The easiest way to do so is using this middleware globally.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class JsonMiddleware
{
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment