Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda