2013-01-11 95 views
0

我能夠成功地使用Event :: Override函數與404事件,但不是500事件。我只希望事件通過flash消息重定向到首頁,因爲我可以很好地處理404個事件。自定義錯誤500頁面不顯示使用Event :: override - Laravel

我還需要做些什麼才能讓500個事件也重定向到我的首頁?請參閱routes.php文件中的代碼:

Event::listen('404', function() { 
    return Response::error('404'); 
}); 

Event::listen('500', function() { 
    return Response::error('500'); 
}); 

Event::override('404', function() { 
    Session::flash('error', '<strong>Error 404 - Not Found.</strong> The page you are  looking for is either <u>invalid</u> or <u>may no longer exist</u>. You are now back at our  home page.'); 
    return Redirect::to_route('home'); 
}); 

Event::override('500', function() { 
    Session::flash('error', '<strong>Error 500 - Internal Server Error.</strong>  Something went wrong on our servers, apologies for that. Please contact us if this  persists.'); 
    return Redirect::to_route('home'); 
}); 

有什麼想法?

+0

肯定的是,有人已經有過這種經歷過嗎?任何人? – user1746582

回答

0

我已經能夠通過直接在\ application \ views \ error文件夾中更新500錯誤文件來解決此問題。

0

我的一個移動應用程序需要格式爲json的錯誤。我從laravel論壇得到了它的解決方案。爲了捕捉HTTP錯誤

App::error(function(Symfony\Component\HttpKernel\Exception\HttpException $exception) { 
    $code = $exception->getStatusCode(); 
    // you can define custome message if you need 
    return Response::json(array(
     'message' => $exception->getMessage() , 
     'code' => $code , 
    ) , $code); 

}); 

捕獲未處理像DB錯誤..

App::error(function(Exception $exception , $code) 
{ 
    return Response::json( array(
     'message' => $exception->getMessage() , 
     'code' => $code , 
    ), $code); 
}); 
相關問題