I'm trying to handle custom errors with laravel 5.3 but can't seem to get the abort(404) to catch correctly.
If I put the abort in my routes (web.php) I get my 404 page.
If i put the abort in the blade, I get a NotFoundHttpException message, but defaults to my fallback error condition, a 500.
app/exceptions/handler.php render function:
// 404 page when a model is not found
if ($exception instanceof ModelNotFoundException or $exception instanceof NotFoundHttpException) {
return response()->view('errors.404', $requestAndException, 404);
}
if ($this->isHttpException($exception)) {
//return $this->renderHttpException($exception);
// mirrored code from renderHttpException to allow for exception-less errors in non-dev mode
$status = $exception->getStatusCode();
if (view()->exists("errors.{$status}")) {
return response()->view("errors.{$status}", $requestAndException, $status, $exception->getHeaders());
} else {
return $this->convertExceptionToResponse($exception);
}
} else {
// Custom error 500 view on production
return response()->view('errors.500', $requestAndException, 500);
}
I tried updating the use section with the specific exceptions, but did not seem to make a difference. Currently sits at:
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
Any ideas?
In you views folder create a folder called "errors", then inside it create your custom views for the exceptions in example: 404.blade.php, 500.blade.php. When you have an abort with the same status of the the name of the view, Laravel will handle it for you, so you don't have to put in in your routes.