Laravel 4 - Handling 404s With Custom Messages

半城伤御伤魂 提交于 2019-12-10 14:46:46

问题


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:


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.




回答2:


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



来源:https://stackoverflow.com/questions/23022632/laravel-4-handling-404s-with-custom-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!