According to Laravel 4 docs I can throw a 404 with a custom response:
App::abort(404, 'My Message');
I can then handle all of my 404s with a custom page:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
How can I pass 'My Message' through to the view in the same way that the generic Laravel error page does.
Thanks!
You can catch your message through the Exception parameter
App::missing(function($exception)
{
$message = $exception->getMessage();
$data = array('message', $message);
return Response::view('errors.missing', $data, 404);
});
Note: The code can be reduced, I wrote it like this for the sake of clarity.
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 into abort()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With