I am using Laravel 5, and I have created a file 404.blade.php
in
views/errors/404.blade.php
This file gets rendered each time I call:
abort(404); // alias of App::abort(404);
How can I pass a custom message? Something like this in 404.blade.php
Sorry, {{ $message }}
Filled by (example):
abort(404, 'My custom message');
or
abort(404, array(
'message' => 'My custom message'
));
In Laravel 4 one could use App::missing
:
App::missing(function($exception)
{
$message = $exception->getMessage();
$data = array('message', $message);
return Response::view('errors.404', $data, 404);
});
How about sharing a variable globally?
Just make sure in the template to fallback to a default in case you forget to set it.
See sharing data with views: http://laravel.com/docs/5.0/views
(Note: copied from my answer here.)
In Laravel 5, you can provide Blade views for each response code in the
/resources/views/errors
directory. For example a 404 error will use/resources/views/errors/404.blade.php
.What's not mentioned in the manual is that inside the view you have access to the
$exception
object. So you can use{{ $exception->getMessage() }}
to get the message you passed intoabort()
.Extend Laravel's Exception Handler,
Illuminate\Foundation\Exceptions\Handler
, and overriderenderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e)
method with your own.If you haven't run
php artisan fresh
, it will be easy for you. Just editapp/Exceptions/Handler.php
, or create a new file.Handler.php
And then, use
$e
variable in your404.blade.php
.i.e.
and in your
404.blade.php
For other useful methods like
getStatusCode()
, referSymfony\Component\HttpKernel\Exception