Create custom view for Error Exception in Laravel 5.4

0

I would like to create custom error views, I have created the locations in Handler.php

but when I have an error: ERROR_EXCEPTION is not redirected to the error view: 500

Should I create some extra view for this error?

my code is:

public function render($request, Exception $exception)
{

    if ($exception->getStatusCode() == 500) {
        return response()->view('errors.500', [], 500);
    }
    if ($exception->getStatusCode() == 404) {
        return response()->view('errors.404', [], 404);
    }
    if ($exception->getStatusCode() == 503) {
        return response()->view('errors.503', [], 503);
    }

    return parent::render($request, $exception);

}
    
asked by Leoh 23.10.2017 в 02:04
source

1 answer

0

You already have the views in the errors folder, then use the abort () method:

if ($exception->getStatusCode() == 500) {
    abort(500);
}
if ($exception->getStatusCode() == 404) {
    abort(404);
}
if ($exception->getStatusCode() == 503) {
    abort(503);
}

Now if you want to show an error view according to an instance of the exception you can do it like this:

if ($exception instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
    abort(404);
}

if ($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) {
    abort(405);
}
    
answered by 26.10.2017 в 03:37