Custom Error 500 page not displaying using Event::override - Laravel

感情迁移 提交于 2019-12-12 01:19:22

问题


I am able to use the Event::Override function successfully with the 404 event but not the 500 event. I simply wish the event to redirect to the front page with a flash message, as I am able to do fine with 404 events.

Is there something else I need to do to get the 500 events also redirecting to my front page? see code below in routes.php file:

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');
}); 

any ideas?


回答1:


I've been able to work around this by updating the 500 error file directly in the \application\views\error folder.




回答2:


One of my mobile application need a error as json format. I got it's solution from laravel forum . For catching http error

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);

});

Catch unhandled like db error, ..

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


来源:https://stackoverflow.com/questions/14276741/custom-error-500-page-not-displaying-using-eventoverride-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!