简体   繁体   English

Laravel 4 - 使用自定义消息处理404s

[英]Laravel 4 - Handling 404s With Custom Messages

According to Laravel 4 docs I can throw a 404 with a custom response: 根据Laravel 4 文档,我可以使用自定义响应抛出404:

App::abort(404, 'My Message');

I can then handle all of my 404s with a custom page: 然后,我可以使用自定义页面处理所有404:

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. 如何以与通用Laravel错误页面相同的方式将“我的消息”传递到视图。

Thanks! 谢谢!

You can catch your message through the Exception parameter 您可以通过Exception参数捕获您的消息

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. 在Laravel 5中,您可以为/resources/views/errors目录中的每个响应代码提供Blade视图。 For example a 404 error will use /resources/views/errors/404.blade.php . 例如,404错误将使用/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. 手册中没有提到的是在视图中你可以访问$exception对象。 So you can use {{ $exception->getMessage() }} to get the message you passed into abort() . 因此,您可以使用{{ $exception->getMessage() }}来获取传递给abort()的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM