I am trying to display a custom error page instead of the default Laravel 5 message :
"Whoops...looks like something went wrong"
I made a lot of search before posting here, I tried this solution, which should work on Laravel 5 but had no luck with it : https://laracasts.com/discuss/channels/laravel/change-whoops-looks-like-something-went-wrong-page
Here is the exact code I have in my app/Exceptions/Handler.php
file :
<?php namespace App\Exceptions;
use Exception;
use View;
use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler;
class Handler extends ExceptionHandler {
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
public function report(Exception $e)
{
return parent::report($e);
}
public function render($request, Exception $e)
{
return response()->view('errors.defaultError');
}
}
But, instead of displaying my custom view, a blank page is showing. I also tried with this code inside render()
function
return "Hello, I am an error message";
But I get same result : blank page
on Larvel 5.2 on your
app/exceptions/handler.php
just extend this methodrenderHttpException
ie add this method tohandler.php
customize as you wishI have two error pages - 404.blade.php & generic.blade.php
I wanted:
I'm using .env - APP_DEBUG to decide this.
I updated the render method in the exception handler:
app/Exceptions/Handler.php
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 entireprepareResponse
function with this one: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
, create500.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: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.The typical way to do this is to just create individual views for each error type.
I wanted a dynamic custom error page (so all errors hit the same blade template).
In Handler.php I used:
Then I don't have to create 20 error pages for every possible http error status code.
Instead of the response create a route for your error page in your Routes.php, with the name 'errors.defaultError'. for example
Either make a controller or include the function in the route with
and use a redirect instead. For example
In laravel 5.4, you can place this code block inside render function in Handler.php - found in app/exceptions/Handler.php