Custom error page not showing on Laravel 5

前端 未结 6 807
旧时难觅i
旧时难觅i 2020-12-19 07:25

I am trying to display a custom error page instead of the default Laravel 5 message :

\"Whoops...looks like something went wrong\"

6条回答
  •  盖世英雄少女心
    2020-12-19 08:00

    I strongly agree with everyone who wants to customize the error experience in Laravel such that their users never see an embarrassing message such as 'Whoops, looks like something went wrong.'

    It took me forever to figure this out.

    How To Customize the "Whoops" Message In Laravel 5.3

    In app/Exceptions/Handler.php, replace the entire prepareResponse function with this one:

    protected function prepareResponse($request, Exception $e)
    {        
        if ($this->isHttpException($e)) {            
            return $this->toIlluminateResponse($this->renderHttpException($e), $e);
        } else {
            return response()->view("errors.500", ['exception' => $e]); //By overriding this function, I make Laravel display my custom 500 error page instead of the 'Whoops, looks like something went wrong.' message in Symfony\Component\Debug\ExceptionHandler
        }
    }
    

    Basically, it's almost identical to the original functionality, but you're just changing the else block to render a view.

    In /resources/views/errors, create 500.blade.php.

    You can write whatever text you want in there, but I always recommend keeping error pages very basic (pure HTML and CSS and nothing fancy) so that there is almost zero chance that they themselves would cause further errors.

    To Test That It Worked

    In routes/web.php, you could add:

    Route::get('error500', function () {
        throw new \Exception('TEST PAGE. This simulated error exception allows testing of the 500 error page.');
    });
    

    Then I would browse to mysite.com/error500 and see whether you see your customized error page.

    Then also browse to mysite.com/some-nonexistent-route and see whether you still get the 404 page that you've set up, assuming you have one.

提交回复
热议问题