Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Make all error status codes return a single custom view

By default, Laravel looks for error views under resources/views/errors, returning the corresponding view for the relevant status code, eg. 404 or 403. Instead of creating all these views manually I want to use my own custom view for all error codes, with the actual error code and message shown dynamically in the view using getMessage() and any other helper functions that might be available to me.

This would allow me to do this as normal:

abort(<statuscode>, <mymessage>)

...but always return just the one view.

Note that what I'm asking is not the same as what's being requested here, which is to force all errors to 404s no matter their actual status code. I want to keep status codes as they should be, just render them all in the same view.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In the default exception handler, a method called getHttpExceptionView() determines the view to return. Simply override it in your App\Exceptions\Handler class with your desired logic.

use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

protected function getHttpExceptionView($e)
{
    if ($e->getStatusCode() === 409) {
        return "exceptions.special";
    }
    return "exceptions.default";
}

You're returning a standard view path, dot-separated if you are using directories.

over 4 years ago · Santiago Trujillo Report

0

As outlined by miken32's answer, I added the following to the bottom of the Handler class inside the App\Exceptions\Handler.php file:

class Handler extends ExceptionHandler
{
    //  Default classes
    //  ...

    protected function getHttpExceptionView($e)
    {
        //  409 errors are handled specially
        //  Remove this if block entirely to serve them with the same error page as below
        if ($e->getStatusCode() === 409) {
            return "exceptions.special"; 
        }
        return "errors.custom"; // Return a "custom" file under "resources/views/errors"
    }
}

I then added the following to my error view:

<h1 class="primary-header">
  @if($exception)
    {{"Error " . $exception->getStatusCode().":"}}
    {{ $exception->getMessage() ? $exception->getMessage() : "page not found" }}
  @else {{ "Generic error: page not found" }}
  @endif
</h1>

This now allows me to use the abort() function in a controller, for example, to throw any HTTP error code with an optional message:

Route::get("/failtest/", function (){
        abort(404, "someone messed up");
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!