Laravel 4 - 使用自定义消息处理404s [英] Laravel 4 - Handling 404s With Custom Messages

查看:106
本文介绍了Laravel 4 - 使用自定义消息处理404s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Laravel 4 docs ,我可以抛出一个自定义响应的404: / p>

  App :: abort(404,'我的消息'); 

然后,我可以使用自定义页面处理所有的404:

  App :: missing(function($ exception)
{
return Response :: view('errors.missing',array() ,404);
});

我如何将我的消息传递给视图,方法与通用Laravel错误



谢谢!

解决方案

你可以抓住你的消息通过异常参数

  App :: missing(function($ exception)
{
$ message = $ exception-> getMessage();
$ data = array('message',$ message);
返回Response :: view('errors.missing',$ data,404);
});

注意:代码可以减少,为了清楚起见,我写了这样的代码。 p>

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.

这篇关于Laravel 4 - 使用自定义消息处理404s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆