The error pages in Laravel should be controlled through the class App\Exceptions\Handler, which is in charge of handling all the exceptions of the framework. Inside this class we have two methods: reportand render.
The method we are interested in is render, which is responsible for converting an exception into an HTTP response. This is where we can define our custom responses. For example, for a response to error code 500 :
public function render($request, Exception $exception)
{
if ($exception instanceof CustomException) {
return response()->view('errors.500', [], 500);
}
return parent::render($request, $exception);
}
A much more dynamic way to redirect to your custom error pages and respond to exceptions (errors) that occur in your application would be the following:
public function render($request, Exception $e)
{
if($this->isHttpException($e)){
if (view()->exists('errors.'.$e->getStatusCode()))
{
return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());
}
}
return parent::render($request, $e);
}
This code snippet checks that the exception being received is HTTP and that the custom view exists before redirecting the user. All this taking into account that the views are located in the directory views/errors/{code}, as it seems to be your case.
I hope I've helped. You can always refer to the official Laravel documentation which is very well explained: Error Handling in Laravel .
The error pages in Laravel should be controlled through the class
App\Exceptions\Handler
, which is in charge of handling all the exceptions of the framework. Inside this class we have two methods:report
andrender
.The method we are interested in is
render
, which is responsible for converting an exception into an HTTP response. This is where we can define our custom responses. For example, for a response to error code 500 :A much more dynamic way to redirect to your custom error pages and respond to exceptions (errors) that occur in your application would be the following:
This code snippet checks that the exception being received is HTTP and that the custom view exists before redirecting the user. All this taking into account that the views are located in the directory
views/errors/{code}
, as it seems to be your case.I hope I've helped. You can always refer to the official Laravel documentation which is very well explained: Error Handling in Laravel .