shlogg · Early preview
Paul Redmond @paulredmond

Laravel 11 Simplifies Returning JSON For Exceptions

Enforce returning JSON on API requests for exceptions in Laravel 11 without middleware. Use `shouldRenderJsonWhen()` method to ensure exceptions are rendered as JSON.

Have you ever enforced returning JSON on API requests for exceptions using a custom middleware like the following:
class ForceJsonResponse{    public function handle(Request $request, Closure $next)    {        $request->headers->set('Accept', 'application/json');         return $next($request);    }}
Laravel 11 gives you a handy way to do this without any extra middleware. I think it's really nice if you are using a web browser to test API routes and always get JSON even without setting the Accept header to application/json or using the above middleware:
// bootstrap/app.php return Applicatio...