[英]Catching error exceptions in Laravel 4
從文檔中我們可以捕獲所有404,如下所示:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
我們還可以做以下事情:
App::abort(404);
App::abort(403);
所有404都由App::missing
處理
所有其他錯誤由以下方式處理:
App::error(function( HttpException $e)
{
//handle the 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
簡短回答:如果您的自定義App :: error函數沒有返回值,Laravel將處理它。 檢查文檔
自定義錯誤視圖和/或邏輯的代碼示例:
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);
}
});
上面的代碼段可以在默認的Log :: error處理程序之后插入到app / start / global.php中 ,或者更好地在自定義服務提供程序的引導方法中插入。
編輯:已更新,因此處理程序僅處理您指定的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.