How to customize the Laravel 4 error page?

Asked

Viewed 1,832 times

1

When an error occurs in Laravel 4, with the configuration of app.debug equal to false, the following page is returned:

inserir a descrição da imagem aqui

In that case, so much for mistakes 500 how much to 404, this page is displayed.

Is there any way to change the page that is displayed when only 400 error occurs?

There is a way to define a view specific to each type of error with a particular http response code?

Example, I want to set an error page to status 404 and another to 500. How do I do that?

1 answer

1


I searched the "Resources" but did not find, however as this link https://driesvints.com/blog/laravel-4-quick-tip-custom-error-pages you can do so:

App::error(function($exception, $code)
{
    $error = array( 'error' => $exception->getMessage());
    switch ($code)
    {
        case 403:
            return Response::view('errors.403', $error, 403);

        case 404:
            return Response::view('errors.404', $error, 404);

        case 500:
            return Response::view('errors.500', $error, 500);

        default:
            return Response::view('errors.default', $error, $code);
    }
});

Any request where the answer is related to a http, the Laravel uses this method App::error to check if you made any customization for certain error. The way this is done is by checking if there is anything being returned by the closure passed as argument from App::error. Should he return null or none (empty Return), Laravel will use the default error (which is Whoops!).

Remembering that when you set up the error page, the call from App::abort() is also affected. That is, if you do this...

return App::abort('Acesso não autorizado', 403);

... It will no longer be called error whoops!, rather what you defined. In this case, the value of the arguments $code and $exception->getMessage() will be the values passed to App::abort();

  • 1

    That’s right. I’ve already implemented here on the system. If you want to ignore any error, in fact just do not return anything, Laravel uses the standard error.

  • @Wallacemaxters got confused about your comment, ignore error and default error.

  • If you want to ignore a specific http error code, just return null. That’s what I meant.

  • @Wallacemaxters thanks for the detailing, I had not gone so deep :)

  • 1

    I’m a fan of Laravel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.