July 2018
Intermediate to advanced
420 pages
8h 46m
English
By default, Laravel has a very powerful error handling engine, but it is totally dedicated to the MVC development pattern, as we mentioned previously. In the next few lines, we'll see how we can change this default behavior and add some specific error handling to our API:
// This will replace our 404 response from the MVC to a JSON response.if ($exception instanceof ModelNotFoundException && $request->wantsJson() // Enable header Accept: application/json to see the proper error msg) { return response()->json(['error' => 'Resource not found'], 404);}if ($exception instanceof MethodNotAllowedHttpException) ...Read now
Unlock full access