[英]Catching error exceptions in Laravel 4
From the documentation it says we can catch all 404 like so: 从文档中我们可以捕获所有404,如下所示:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
And we can also do things like: 我们还可以做以下事情:
App::abort(404);
App::abort(403);
All 404 gets handled by the App::missing
所有404都由
App::missing
处理
All other errors gets handled by: 所有其他错误由以下方式处理:
App::error(function( HttpException $e)
{
//handle the error
});
But the question is How do i handle each of the error like if its a 403 I will display this if its a 400 I will display another error
但问题是
How do i handle each of the error like if its a 403 I will display this if its a 400 I will display another error
Short answer: if your custom App::error function does not return a value, Laravel will handle it. 简短回答:如果您的自定义App :: error函数没有返回值,Laravel将处理它。 Check the docs
检查文档
Code sample for custom error views and/or logic: 自定义错误视图和/或逻辑的代码示例:
App::error(function(Exception $exception, $code){
// Careful here, any codes which are not specified
// will be treated as 500
if ( ! in_array($code,array(401,403,404,500))){
return;
}
// assumes you have app/views/errors/401.blade.php, etc
$view = "errors/$code";
// add data that you want to pass to the view
$data = array('code'=>$code);
// switch statements provided in case you need to add
// additional logic for specific error code.
switch ($code) {
case 401:
return Response::view($view, $data, $code);
case 403:
return Response::view($view, $data, $code);
case 404:
return Response::view($view, $data, $code);
case 500:
return Response::view($view, $data, $code);
}
});
The above snippet could be inserted in app/start/global.php after the default Log::error handler, or better yet in the boot method of a custom service provider . 上面的代码段可以在默认的Log :: error处理程序之后插入到app / start / global.php中 ,或者更好地在自定义服务提供程序的引导方法中插入。
EDIT: Updated so the handler only processes codes you specify. 编辑:已更新,因此处理程序仅处理您指定的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.