Custom Error 500 page not displaying using Event::

2019-08-20 23:07发布

问题:

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