5

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!

1
  • @totymedli I wanted to specify the message in the 'abort' function, rather than in the generic handler, so I can use multiple messages. I was just missing the $exception->getMessage() line. Thanks for your help. Commented Apr 12, 2014 at 11:59

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I was after! Thanks very much.
In what file(s) and folder(s) do you have to add this code, @LukeJoyce ??
@pathros, if you are using Laravel 4.x this should be on app.php, I think it is similar for Laravel 5.
4

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().

1 Comment

Can't be upvoted enough. I like Laravel, but it's "read the framework" approach to documentation can be a little frustrating.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.